Dispose Question

H

H2Os

Hi

I have a Time Picker control written in VB.NET which has been cobbled
together from various examples found all over the web. It uses P/Invoke to
call CreateWindowEx and stores the window handle so that it can use
SendMessage to communicate with the physical window and seems to works fine.

My question is this: At what point is the window destroyed and the handle
released? I have code in Dispose(Boolean) to call DestroyWindow, but it is
(apparently) never called (I have put some code to write out to a file and
it never happens).

Does the CLR handle destroying the window and if so is it when the control
is GC'd or when the app exits? Or, should I be doing it somewhere else and
the CLR is orphaning it?

Any thoughts or solid knowledge would be gratefully received!
 
G

Guest

If you have properly implemented IDisposable interface, your finalizer will
be called by GC when your application is closed.
 
H

H2Os

Thanks for the reply Alex.

However, I still have some problems with the Dispose method not being
called. Below is the dispose and destructor code that I have implemented and
the dispose method never gets called - only Finalize. If I try to call
DestroyWindow from Finalize the app hangs and I have to kill it. I realise
that I should only have the code in one place or the other (not both) but
this is just to illustrate the point.

Public Sub Dispose() Implements System.IDisposable.Dispose
' This does not get called
Dim outFile As IO.StreamWriter = IO.File.AppendText("\DTDisp.txt")
outFile.WriteLine("Dispose - DestroyWindow(" & Me_hWnd.ToString & ")")
outFile.Close()
If Not IntPtr.Zero.Equals(Me_hWnd) Then DestroyWindow(Me_hWnd)
Me_hWnd = IntPtr.Zero
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
' This does get called but fails on DestroyWindow
Dim outFile As IO.StreamWriter = IO.File.AppendText("\DTDisp.txt")
outFile.WriteLine("Finalize - DestroyWindow(" & Me_hWnd.ToString & ")")
outFile.Close()
If Not IntPtr.Zero.Equals(Me_hWnd) Then DestroyWindow(Me_hWnd)
Me_hWnd = IntPtr.Zero
MyBase.Finalize()
End Sub

Is this a 'properly implemented IDisposable interface' and if not, what
needs to change?


Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top