Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to dispose Hwnd IntPtr

I'm trying to figure out whether the following situation produces a memory leak or not.

I am getting a window handle from a WebBrowser control (there is a reason for this)

IntPtr p = webBrowser1.FindForm().Handle;

Later, I am releasing that handle in an overridden dispose method

    protected override void Dispose(bool disposing)
    {
        hwnd = IntPtr.Zero;
        base.Dispose(disposing);
    }

Is there something evil behind my Dispose method that might cause a memory leak?

This might be pretty obvious to some, but I am hunting memory leaks so I'm trying to verify every line of code.

like image 499
iCantSeeSharp Avatar asked Feb 18 '23 19:02

iCantSeeSharp


1 Answers

There is no significance in setting your HWND to IntPtr.Zero.

IntPtr is a value type, so having the value in that variable has no effect on anything. It does not keep any object alive. It's just a number that you are setting to 0.

If you got the HWND from an existing window, then no handle should have been duplicated and you don't have to release it. The HWND will be destroy when the form is closed.

like image 169
Ran Avatar answered Feb 24 '23 01:02

Ran