Re: WebResponse.Close() not disposing unmanaged resource?




Hi Chris,

don't know if makes any change, but the WebResponse class implements the
IDisposable interface, so you could try a using statement:

using (System.Net.WebResponse resp=req.GetResponse()) {
// do some stuff with resp if you want.
}

If there's any cleanup code in the WebRespone.Dispose implementation, it
will be executed as soon as the using block is left and it will be executed
even if an exception is thrown.

Michael



<cmbardon@xxxxxxxxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:1131564602.694642.104070@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>I have a .net app that uses a lot of calls to System.Net.WebRequest,
> and it appears to be leaking memory. I've spent a couple of days
> working with the excellent .net memory profiler from Scitech
> (http://www.scitech.se/) trying to find the leak, and all I've managed
> to find is that the win32 (unmanaged) heap seems to keep growing. I
> wrote this (pretty non-realistic) code in a test app to try duplicating
> the problem:
>
> while(true)
> {
> try
> {
> System.Net.WebRequest
> req=System.Net.WebRequest.Create(@"http://chris/TestWebService/TestService.asmx?WSDL";);
> System.Net.WebResponse resp=req.GetResponse();
> resp.Close();
> }
> catch(Exception ex)
> {
> MessageBox.Show(ex.ToString());
> }
> }
>
>
> Note that this was just inside a button click event of a winform
> application. The exception condition never got hit, and the app just
> kept hammering away at my local web server. Checking the profiler, I
> saw the number of undisposed instances of System.Net.HttpWebResponse,
> System.Net.ConnectStream, and System.Threading.ManualResetEvent
> increasing with each iteration. Shouldn't the resp.Close() statement
> release the resources? There's nothing in the documentation about
> doing any sort of disposal on the request object, so it appears that
> only the response needs to be disposed, and so far, that doesn't seem
> to be happening. I've also tried the above code casting the response to
> an HTTPWebResponse, but that has no effect. I also tried removing the
> resp.Close() statement, which had no effect (the undisposed objects
> still showed up in the profiler). Is there something else I need to do
> to fix this memory leak?
>
> Thanks for your help,
>
> Chris
>


.