Page 1 of 1

wxwindows MDI Eg: clarification

Posted: Sat Sep 11, 2004 8:40 pm
by akkumar
Hi,
I was going through the example MDI program
( in $WXDIR/samples/mdi directory).

<-- Begin code fragment -->

void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
{
// Make another frame, containing a canvas
MyChild *subframe = new MyChild(frame, _T("Canvas Frame"),
wxPoint(-1, -1), wxSize(-1, -1),
wxDEFAULT_FRAME_STYLE);

.
.
subframe->Show(TRUE);
}

<-- End code fragment -->

We are allocating memory for subframe here. ( of type MyChild).
But we are not freeing it. When would it be freed ?

If I were to close the document window corresponding to this MDI Child Frame, would this memory be freed ?

If not, how would prevent a probably memory leak in this example .
Assume I invoke this function N number of times, would i be leaving behind a memory leak every time the function is invoked (assume, I close the windows after invocation manually ).

-- Karthik.

PS: I had posted this previously to comp.soft-sys.wxwindows . Since I did not get any reply there, I am posting it here. Sorry if you feel this is cross-posting

Re: wxwindows MDI Eg: clarification

Posted: Sat Sep 11, 2004 8:52 pm
by Ryan Norton
akkumar wrote: We are allocating memory for subframe here. ( of type MyChild).
But we are not freeing it. When would it be freed ?
When the parent is deleted-
http://wiki.wxwidgets.org/wiki.pl?Avoiding_Memory_Leaks

Posted: Sun Sep 12, 2004 5:34 am
by akkumar
But when I try to access the child window after closing the child window, I get a seg. fault.
The scenario I have is :-

* Two child windows each having a wxGLCanvas in them.

* The child windows may be closed but I dont want them to be destroyed from memory . I would like to 'hide' the window when it is closed but there could be another menu-driven event that can make this window pop up (with it not being destroyed when it was closed ).

I was wondering how i could implement that ?

Posted: Sun Sep 12, 2004 6:13 am
by Ryan Norton
akkumar wrote:But when I try to access the child window after closing the child window, I get a seg. fault.
The scenario I have is :-

* Two child windows each having a wxGLCanvas in them.

* The child windows may be closed but I dont want them to be destroyed from memory . I would like to 'hide' the window when it is closed but there could be another menu-driven event that can make this window pop up (with it not being destroyed when it was closed ).

I was wondering how i could implement that ?
I may be missed something obvious here, but why not just call Hide() on the child window instead of closing it?

A more hackish way is to override the close event of the window and not delete it.

If you want to delete the parent window and keep the children around you'll want to call Reparent(NULL) on the child windows before deleting the parent (NOTE: Windows and GTK-specific).