views:

25

answers:

1

I've been trying to debug why closing windows forms designer is crashing visual studio and have discovered by adding a series of message boxes that if my controls are nested inside a System::Windows::Forms::TabControl the destructor of those controls are called twice.

Is that expected behaviour and is there a way of avoiding it?

+1  A: 

The Dispose() method may be called more than once, there is no rule that says it isn't legal. If you do override it (implement the destructor in C++/CLI) then you have to make sure that your code is resilient to this. Very commonly done with an isDisposed field in the class.

Hans Passant
Is it better/more standard to implement/override the dispose method or to do this in a constructor? ..or is this yet another C++CLI ambiguity?
Jon Cage
Mapping Dispose() to the destructor was an unfortunate choice in my book. Trying too much to make it look like C++, but not getting the same behavior. The language won't allow you to implement it explicitly. But take a step back, implementing Dispose() isn't often needed in a UserControl. It already disposes the constituent controls automatically.
Hans Passant
The difficulty I'm having is that my user controls have background workers running to acquire data. These need to be shutdown cleanly when the app is closing. Is that the sort of place you might use the dispose method?
Jon Cage
It's too late, the BGWs will need time to spin down. You cannot wait, that's a guaranteed deadlock. You need help from the form, it needs to delay the close.
Hans Passant
BTW: solve your original problem by *not* starting the BGWs in design mode. Use the DesignMode property.
Hans Passant
Thanks Hans - once again to my rescue! I owe you a beer or two :-) Just destroying the threads carefully when closing the GUI components was what I needed.
Jon Cage