Windows Mobile:Finalizer中的异常终止程序

时间:2022-06-22 20:30:45

My Windows mobile application crashes sometimes with an exception that happens in the finalizer of System.Net.HttpReadStream.

我的Windows移动应用程序有时会因System.Net.HttpReadStream的终结器中发生异常而崩溃。

It only happens sometimes, but then it brings down the whole program. Is there anything I can do to make the program continue when such an internal finalizer throws? Or alternatively, how can I prevent such an error?

它有时只会发生,但随后它会降低整个程序。当这样的内部终结器抛出时,有什么办法可以让程序继续运行吗?或者,我该如何防止出现这样的错误?

Here's the stacktrace of the exception (not complete, since I have to type the whole thing...

这是异常的堆栈跟踪(不完整,因为我必须输入整个东西......

ObjectDisposedException

at System.Threading.Timer.throwIfDisposed()
at System.Threading.Timer.Change(UInt32 dueTime, UInt32 period)
at ...
at System.Net.ContentLengthReadStream.doClose()
at System.Net.HttpReadStream.Finalize()

The calling code is:

调用代码是:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(actionUrl);
request.AllowAutoRedirect = true;
request.AllowWriteStreamBuffering = true;
request.Method = "POST";
request.ContentType = "application/json";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(writer, myRequestObject);
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        JsonSerializer serializer = new JsonSerializer();
        MyResultObject result = (MyResultObject)serializer.Deserialize(reader, typeof(MyResultObject));

        return result;
    }
}

Update

The calling code above is fine. The problem was caused by another HttpWebRequest where the response was not disposed. So remember, always dispose the response object, but especially in Compact Framework since it can bring down your whole application!

上面的调用代码很好。问题是由另一个没有处理响应的HttpWebRequest引起的。所以请记住,始终处置响应对象,尤其是在Compact Framework中,因为它可以降低整个应用程序!

3 个解决方案

#1


This thread describes a similar problem.

该线程描述了类似的问题。

The problem could be that you are returning inside the second using block. This way the response object won't get closed.

问题可能是你在第二个使用块内返回。这样响应对象就不会被关闭。

#2


It won't hurt to close (Dipsose) the ResponseStream explicitly, as in:

显式关闭(Dipsose)ResponseStream不会有什么坏处,如:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream respStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(respStream))
{
  JsonSerializer serializer = new JsonSerializer();
  MyResultObject result =
    (MyResultObject)serializer.Deserialize(reader, typeof(MyResultObject));

  return result;
}

Otherwise it is up to the Reader object to close it's stream, a bit flaky.

否则,由Reader对象关闭它的流,有点片状。

#3


It turned out that it was another HttpWebRequest causing this. The code in my question was fine, but the dispose of the response was missing in the other place.

事实证明,这是另一个导致此问题的HttpWebRequest。我的问题中的代码很好,但在另一个地方丢失了响应的处理。

#1


This thread describes a similar problem.

该线程描述了类似的问题。

The problem could be that you are returning inside the second using block. This way the response object won't get closed.

问题可能是你在第二个使用块内返回。这样响应对象就不会被关闭。

#2


It won't hurt to close (Dipsose) the ResponseStream explicitly, as in:

显式关闭(Dipsose)ResponseStream不会有什么坏处,如:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream respStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(respStream))
{
  JsonSerializer serializer = new JsonSerializer();
  MyResultObject result =
    (MyResultObject)serializer.Deserialize(reader, typeof(MyResultObject));

  return result;
}

Otherwise it is up to the Reader object to close it's stream, a bit flaky.

否则,由Reader对象关闭它的流,有点片状。

#3


It turned out that it was another HttpWebRequest causing this. The code in my question was fine, but the dispose of the response was missing in the other place.

事实证明,这是另一个导致此问题的HttpWebRequest。我的问题中的代码很好,但在另一个地方丢失了响应的处理。