Page 1 of 1

Using std::shared_ptr with wxWidgets

Posted: Mon Mar 14, 2011 10:17 am
by Vexator
Usually, wxWidgets seems to handle pointers quite well. I'm using it now to create a level designer for my graphics engine which uses shared_ptr's for everything and I'd like to use them for my wxWidgets windows as well as I don't like mixing raw and managed pointers.

What's the correct way of doing this in wxWidgets?

Posted: Mon Mar 14, 2011 11:53 am
by catalin
I'd say you can use shared_ptr with wxWindow but you shouldn't need to.
Deleting a wxWindow is always handled by its parent, and parent-less ones (top level windows) are normally deleted by the app. So you don't need to manage the pointers of wxWindow-s.
Maybe there are exceptions, like context menus, but they can be created on the stack so again, no managing is necessary.

You can however delete a wxWindow on your own, the only drawback should be that a wxWindowDestroyEvent will not be sent for that window

Posted: Mon Mar 14, 2011 3:22 pm
by Auria
You would need a special shared pointer implementation; it would need to call ->Destroy() and not delete, and as well detach the window from any sizer it was added to.

I wouldn't recommend trying to do it this way, except for top-level components perhaps

Posted: Mon Mar 14, 2011 5:19 pm
by catalin
Auria wrote:detach the window from any sizer it was added to.
Actually this and all other cleanup is done in ~wxWindowBase() so using delete is safe enough. But I'm not sure how everything will react to any unprocessed events that target the window being deleted.