wxSizers pointers create memory leaks in VS2008

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
valiyuneski
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Dec 11, 2008 3:01 pm

wxSizers pointers create memory leaks in VS2008

Post by valiyuneski » Thu Jan 15, 2009 4:20 pm

After creating a sizer, allocating it and not deleting it,
memory leaks remain in VisualStudio 2008.
Trying to delete them fails to close the window in which they were created.

Any ideas how to release otherwise the memory ??

framepointer
Super wx Problem Solver
Super wx Problem Solver
Posts: 264
Joined: Mon Aug 07, 2006 3:25 pm
Location: Baia Mare, Romania
Contact:

Post by framepointer » Thu Jan 15, 2009 8:31 pm

Hi.

How does your code look?

If you create a sizer on the (unsing new), it's a good idea to assign that sizer to a control/window (something that inherits wxWindow) using the SetSizer() method
http://docs.wxwidgets.org/stable/wx_wxw ... owsetsizer


This way the sizer will be deleted automatically when the control/window is destroy.

If you create more sizers, you can add them as child to the main sizer of a control/window.
Make sure you add a sizer as a child only once to another sizer.

If you follow these guidlines, your sizers will always be freed.

Don't leave anything "hanging in the air". Just because you create a sizer inside a control/class will not assign that sizer to that control. If you do need such sizers for whatever reasons, make sure you save the pointer to it and delete it when you no longer need it.


Hope this helps.



Regards
Last edited by framepointer on Thu Jan 15, 2009 8:33 pm, edited 1 time in total.
Software is like sex,
It's better when it's free.
~Linus Torvalds

radcapricorn
Experienced Solver
Experienced Solver
Posts: 70
Joined: Fri Nov 07, 2008 4:25 pm
Location: Saint-Petersburg, Russia

Post by radcapricorn » Thu Jan 15, 2009 8:32 pm

Can you describe what wxWidgets version you're using and how exactly did you determine those leaks?
win xp pro sp3/VS Express 2008/MinGW;
win Vista Ultimate/VS 2005;
Debian Lenny/gcc/cegcc-mingw32ce;
wxWidgets-2.8.9 w/wxWinCE;

valiyuneski
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Dec 11, 2008 3:01 pm

Post by valiyuneski » Thu Jan 15, 2009 9:26 pm

I use wxFormBuilder to create the dialog:

xx.h
wxBoxSizer* m_pWholeSizer;

xx.cpp
Create()
{
m_pWholeSizer = new wxBoxSizer(wxVERTICAL);
SetSizer(m_pWholeSizer);
}

Delete()
{
if(m_pWholeSizer)
{
delete m_pWholeSizer;
m_pWholeSizer = NULL;
}
}

and that delete is called in destructor, if i do not perform
the pointer deletion everything closes corectly but then i VS2008 shows memory leak.

If i delete it it hangs on delete itself now the dialog has not been destroyed yet but its internal controls have already deleted by myself and now on delete sier attempt
it fails inside wxSizer Destructor.

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Fri Jan 16, 2009 6:09 pm

Is the dialog created on the stack of the heap?

If you are creating it on the heap with new, you shouldn't delete it. But call wxDialog::Destroy() instead, as it will take care of deleting all of its children.

Also as has already been asked but not to clearly answered. How exactly are you determining that there is a leak?

-Max

Post Reply