Problem with event handling

  • Thread starter Thread starter MLM450
  • Start date Start date
M

MLM450

I am using a C# control in an unmanaged C++ app. The control has events
that I need to handle. The first time I present the dialog containing
the control, everything works fine - including the event handlers.
The second time I try to display the window, it crashes before the
dialog displays. It goes down in the GetNextDlgTabItem function because
it fails the IsWindow assert (m_hWnd is null). Why would this happen
and how do I fix this?

I have set up my event handlers using DECLARE_DISPATCH_MAP,
DECLARE_INTERFACE_MAP, and AfxConnectionAdvise/AfxConnectionUnadvise.
It must be related to the event handlers because when I remove the
Advise/Unadvise calls, the problem goes away. The dialog is completely
destroyed after the first iteration - it is a variable within a
function that ends after the dialog is closed.

It seems like there must be something interfering from the first time I
present the dialog, but what is it?

Any ideas?

Thanks!
 
I found it. There must have been something remaining from the previous
instance of the control that was waiting to be deleted. I forced
garbage collection and that seemed to fix the problem.
 
I found it. There must have been something remaining from the previous
instance of the control that was waiting to be deleted. I forced
garbage collection and that seemed to fix the problem.

That's almost never the right solution, however. Is your control
IDisposable? Did you dispose it when you were done?

-cd
 
Thanks for your reply!

My control is a UserControl, so it is IDisposable. I tried calling
Dispose instead of garbage collection and it did not fix the problem. I
was thinking the mix of managed and unmanaged code was cause? Perhaps
the GC did not know it was time to do its work?

If you think of another solution, please let me know.

Thanks,
Mike
 
Back
Top