Page 1 of 1

Destroy call on wxHyperlinkCtrl crashes the application

Posted: Tue Sep 04, 2018 5:37 pm
by deepti
Hi All,

I have an instance of a wxScrolledWindow, which contains several wxBoxSizer instances.
Each wxBoxSizer contains a wxCollapsiblePane and a wxHyperlinkCtrl ("Remove" link). Please refer to attachment. That is how it looks like.
When the "Remove" hyper-link is clicked, the wxCollapsiblePane associated with it (and it's child controls) needs to be removed and also the "Remove" link itself.
For this, I have the following code, which works fine on Windows, but crashes on Mac. If i comment out the line "removeLink->Destroy();", it does not crash, but the link remains, which i obviously do not want.
The wxHyperlinkCtrl is actually customized in order to store the ID of the associated wxCollapsiblePane.
The "m_metaDataControlsbox" is the outermost sizer, which contains all the other wxBoxSizer instances.
What is it that I am doing wrong here? Please help!

Code: Select all

void PropertiesDialog::OnRemoveMetadataControls(wxHyperlinkEvent& event)
{
	bool bRemoved = false;

	int id = event.GetId();
	customHyperLinkCtrl* removeLink = (customHyperLinkCtrl*)FindWindow(id);
	int siblingCollPaneID = 0;
	siblingCollPaneID = removeLink->getSiblingCollPaneID();
	metadataSetCollapsiblePane* cpWindow = (metadataSetCollapsiblePane*)FindWindow(siblingCollPaneID);

	
	if (cpWindow != NULL)
	{	

			wxWindow* cpPane = cpWindow->GetPane();
			wxWindowList cpChildren = cpPane->GetChildren();
			for (int i = 0; i < cpChildren.GetCount(); i++)
			{
				wxWindow* w = cpChildren[i];
				if (w != NULL)
					w->Destroy();
			}
			cpWindow->Destroy();

			removeLink->Destroy();

			wxSizer* cpContainer = cpWindow->GetContainingSizer();
			m_metaDataControlsbox->Remove(cpContainer);
	}
}
issue.jpg
issue.jpg (35.33 KiB) Viewed 372 times

Re: Destroy call on wxHyperlinkCtrl crashes the application

Posted: Tue Sep 04, 2018 6:44 pm
by doublemax
Just deleting the metadataSetCollapsiblePane should be enough, it will destroy all its children (and grandchildren etc).

But as you're inside an event handler for the wxHyperlinkCtrl, you should not cut the branch you're sitting on.

Use wxAppConsole::ScheduleForDestruction to destroy the pane:
https://docs.wxwidgets.org/trunk/classw ... 22f9de9d90

Re: Destroy call on wxHyperlinkCtrl crashes the application

Posted: Tue Sep 04, 2018 6:51 pm
by deepti
Thank you @doublemax.
Use wxAppConsole::ScheduleForDestruction to destroy the pane:
Are you referring to scheduling the destruction of the "Remove" hyperlink here?

Re: Destroy call on wxHyperlinkCtrl crashes the application

Posted: Tue Sep 04, 2018 6:52 pm
by doublemax
As the hyperlink is a child of the pane, i don't think you have to destroy it individually. It'll get destroyed anyway when the pane gets destroyed.

Re: Destroy call on wxHyperlinkCtrl crashes the application

Posted: Tue Sep 04, 2018 6:56 pm
by deepti
The hyperlink is not the child of the pane. It is a sibling, in the sense the pane and hyperlink are both in the same sizer.
Will just hiding the hyperlink help in my case? Once the dialog is closed, any control which has not been explicitly destroyed will anyway be destroyed automatically, isn't it?

Re: Destroy call on wxHyperlinkCtrl crashes the application

Posted: Tue Sep 04, 2018 6:59 pm
by doublemax
The hyperlink is not the child of the pane. It is a sibling, in the sense the pane and hyperlink are both in the same sizer.
Ok, then you should also destroy it using ScheduleForDestruction.
Will just hiding the hyperlink help in my case? Once the dialog is closed, any control which has not been explicitly destroyed will anyway be destroyed automatically, isn't it?
Yes, but it sounds dirty. I wouldn't like to have invisible zombie controls laying around :)

Re: Destroy call on wxHyperlinkCtrl crashes the application

Posted: Tue Sep 04, 2018 7:02 pm
by deepti
Great!! Thanks a lot, my savior! :D