Strange exception in mscorlib with CultureInfo using code converted to .NET 2.0
I recently converted an application that was originally developed in VS 2003 and .NET 1.1 to .NET 2.0 using VS 2005. I've faced many problems with the conversion but was able to resolve all of them except this one. I hope this is the right forum to post this question in. If it's not, please direct me to the appropriate location.
This problem is not repeatable because the failure is inconsistent in where
it appears. Sometimes the error happens in one place, and sometimes it doesn't,
and then happens a few lines later - with the exact same settings, situation,
etc.
This is the code that generally causes the problem:
private CultureInfo DeserializeCultureInfo(byte[] cultureData) {
MemoryStream stm = new MemoryStream(cultureData);
BinaryFormatter bf = new BinaryFormatter();
CultureInfo ci = null;
try
{
ci = (CultureInfo)bf.Deserialize(stm);
}
catch
{
}
stm.Close();
return(ci);
}
The non-repeatability comes from the fact that sometimes the error
(NullReferenceException) occurs on the
ci=(CultureInfo)bf.Deserialize(stm);
line,
and sometimes it occurs upon returning from the function on the
CultureInfo cultureInfo = DeserializeCultureInfo(cultureData); line
and sometimes it happens on some random line that is completely inoccuous
somewhere in that area - for instance the "{" line after the "catch" statement -
as if the error is in another thread, and while the debugger moves along, the
mscorlib code crashes a little bit later.
The byte array cultureData has been viewed during Debug and does not appear
to be out of the ordinary in any way.
source: "mscorlib"
Stack trace:
at
System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage
msg, IMessageSink replySink)
at
System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(Object
o)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)

