wxwindows MDI Eg: clarification

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
akkumar
Earned a small fee
Earned a small fee
Posts: 15
Joined: Sat Sep 11, 2004 8:35 pm
Location: CA

wxwindows MDI Eg: clarification

Post 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
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: wxwindows MDI Eg: clarification

Post 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
[Mostly retired moderator, still check in to clean up some stuff]
akkumar
Earned a small fee
Earned a small fee
Posts: 15
Joined: Sat Sep 11, 2004 8:35 pm
Location: CA

Post 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 ?
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post 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).
[Mostly retired moderator, still check in to clean up some stuff]
Post Reply