Is there automatic dynamic Unbinding upon windows deletion?

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
Martin Silenus
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Nov 05, 2019 8:33 pm

Is there automatic dynamic Unbinding upon windows deletion?

Post by Martin Silenus » Sun Apr 18, 2021 11:41 am

In my application, I can create new modules during runtime. A module is an internal application data class, nothing to do with wxWidgets. Each time a new module is created, a wxToggleButton appears on a panel, added to a sizer on that panel. Because I don't know how many buttons will be present during runtime, I bind my event handlers dynamically during runtime. For example :

Code: Select all

void TMyClass::AddModuleButton(void)
{
	// create dynamicall a new toggle button
	wxToggleButton* NewModuleButton = new wxToggleButton(... the usual stuff...);
	
	// bind a right click menu to the button
	NewModuleButton->Bind(wxEVT_CONTEXT_MENU, &TMyClass::OnMyClassContextMenu, this, NewModuleButton->GetId());
	
	// add it to the sizer of my button panel
   	 BoxSizerModules->Add(NewModuleButton, 0, wxEXPAND, 0);
	
	// add a function for a regular left mouse button click on the button
    	Bind(wxEVT_TOGGLEBUTTON, &TMyClass::OnButtonModuleClick, this, NewModuleButton->GetId());
}
Then I also have a function to delete all the toggle buttons again from the panel again, e.g. when loading a new project file.

Code: Select all

void TMyClass::ClearModules(void)
{
	BoxSizerModules->Clear(true); // from wx documentation : if argument delete_windows = true then child windows will also be deleted
}
So by setting the argument as 'true' when calling the sizer clear function, it also deletes the toggle buttons, which is what I want. What I'm wondering now is if the sizer clear function also takes care of the dynamic bindings? More specifically the one attached to the button itself in :

Code: Select all

NewModuleButton->Bind(wxEVT_CONTEXT_MENU, &TMyClass::OnMyClassContextMenu, this, NewModuleButton->GetId());
Does the Sizer->Clear function also automatically Unbind any existing bindings or do I have to do this manually? Do I actually care about this, or is this to be seen as a memory leak?


Using wxWidgets 3.0.5 in C++

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5154
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Is there automatic dynamic Unbinding upon windows deletion?

Post by ONEEYEMAN » Sun Apr 18, 2021 1:32 pm

Hi,
You shouldn't care about Unbind()'ing the event handler. Everything will be destroyed properly.

Try it and get back if it doesn't

Thank you.

Martin Silenus
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Nov 05, 2019 8:33 pm

Re: Is there automatic dynamic Unbinding upon windows deletion?

Post by Martin Silenus » Sun Apr 18, 2021 4:15 pm

Yes, it does work, I get the behaviour I want. Except I wasn't sure whether I was collecting some "free floating bindings" somewhere that in the long run would add up, perhaps slowing down my app or create a memory leak. I didn't find anything in the documentation about that, if a wx-destructor also automatically unbinds any event handlers and I'm not sure if and how I can find out on my own (other than looking at the memory occupation of my app in Windows task manager, which only tells me so much).
Ok, I'll assume everything is taken care of by wxWidgets and won't interfere myself then.

Post Reply