Using std::shared_ptr with wxWidgets

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
Vexator
I live to help wx-kind
I live to help wx-kind
Posts: 187
Joined: Sun Jan 30, 2005 2:50 pm
Location: Heidelberg, Germany

Using std::shared_ptr with wxWidgets

Post by Vexator » Mon Mar 14, 2011 10:17 am

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?
Windows 7 Pro
Visual Studio 2010
wxWidgets 2.9.3

catalin
Moderator
Moderator
Posts: 1586
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Post by catalin » Mon Mar 14, 2011 11:53 am

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

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Mon Mar 14, 2011 3:22 pm

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
"Keyboard not detected. Press F1 to continue"
-- Windows

catalin
Moderator
Moderator
Posts: 1586
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Post by catalin » Mon Mar 14, 2011 5:19 pm

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.

Post Reply