Dialogs and Destroy() Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
Peterj
Knows some wx things
Knows some wx things
Posts: 38
Joined: Mon Nov 14, 2005 6:48 pm
Location: Australia

Dialogs and Destroy()

Post by Peterj » Thu Aug 10, 2006 11:16 am

Could someone correct my misunderstanding:

I have created several modal dialogs which usually include a Cancel button to which I attach an onclick event. Inside the event I usually put:

Code: Select all

Destroy(); 
to close the dialog.

However this appears to cause the dialogs to close not only themselves, but also the window they were activated from. Running debug I have found using Destroy() has caused an access violation and everything is being closed.

So I have got rid of the event, and set the button ID to wxID_CANCEL. Now my dialog closes as it should. (I also note that using wxID_CLOSE does not close the dialog.)

I understand that the best way to reclaim memory is to use Destroy(), so I have put it into the dialog destructor. It doesn't appear to cause any bad consequences, but is it a valid use of Destroy()?

If anyone could shed any light on the problem, or fill in some gaps in my understanding, it would be appreciated.

Peter
Using Win XP, Dev C++ 4.9.9.2 wx-beta 6.9

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Re: Dialogs and Destroy()

Post by upCASE » Thu Aug 10, 2006 11:31 am

Hi!
Peterj wrote:I understand that the best way to reclaim memory is to use Destroy(), so I have put it into the dialog destructor. It doesn't appear to cause any bad consequences, but is it a valid use of Destroy()?

If anyone could shed any light on the problem, or fill in some gaps in my understanding, it would be appreciated.
In general this correct for controls created on the heap (using new) instead of on the stack. Normaly you wouldn't create a modal dialog on the heap. You simply do somehting like

Code: Select all

MyDialog dlg(...);
dlg.ShowModal();
and don't call Destroy explicitly. It will be called by th dtor of the dialog as soon as it goes out of scope.
To close a modal dialog, call EndModal() from it and pass the return code.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Peterj
Knows some wx things
Knows some wx things
Posts: 38
Joined: Mon Nov 14, 2005 6:48 pm
Location: Australia

Post by Peterj » Thu Aug 10, 2006 11:48 am

Hi upCase,

Thanks for your reply.

I forgot to mention that these are created on the heap. Some of the dialogs get reasonably complicated so I have been under the impression they would be better created there.

In this situation, is it valid to put Destroy() in the dtor?

When the access violation error occurs, debug points to wxEventTableEntry in event.h

Peter
Using Win XP, Dev C++ 4.9.9.2 wx-beta 6.9

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Thu Aug 10, 2006 11:56 am

Peterj wrote:I forgot to mention that these are created on the heap. Some of the dialogs get reasonably complicated so I have been under the impression they would be better created there.

In this situation, is it valid to put Destroy() in the dtor?

When the access violation error occurs, debug points to wxEventTableEntry in event.h
No matter how complicated the dialogs are, if you can create them on the stack when using ShowModal().
Anyway: Don't call Destroy() from the dialog itself. Call it from the calling method after ShowModal() returned.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Peterj
Knows some wx things
Knows some wx things
Posts: 38
Joined: Mon Nov 14, 2005 6:48 pm
Location: Australia

Post by Peterj » Fri Aug 11, 2006 10:15 am

Thanks for the reply. I'll move the Destroy() and check out the difference.

Peter
Using Win XP, Dev C++ 4.9.9.2 wx-beta 6.9

Post Reply