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
wxwindows MDI Eg: clarification
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: wxwindows MDI Eg: clarification
When the parent is deleted-akkumar wrote: We are allocating memory for subframe here. ( of type MyChild).
But we are not freeing it. When would it be freed ?
http://wiki.wxwidgets.org/wiki.pl?Avoiding_Memory_Leaks
[Mostly retired moderator, still check in to clean up some stuff]
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 ?
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 ?
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
I may be missed something obvious here, but why not just call Hide() on the child window instead of closing it?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 ?
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]