Memory Management of a panel using unique_ptr

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
escanor
Earned a small fee
Earned a small fee
Posts: 14
Joined: Sun Jun 10, 2018 1:57 am

Memory Management of a panel using unique_ptr

Post by escanor » Sun Jun 07, 2020 4:02 am

Hi All,

Is it safe to use smart pointers with wxWidgets?

Say I have something like this

Code: Select all

panel = std::make_unique<wxPanel>(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
and then I have a wxTextCtrl that has this panel as its parent.

I know that in my destructor if I call panel->Destroy() it will deallocate all the components that are associated with the panel.

However, the smart pointer does not call ::Destroy, it instead just deletes the panel when it is out of scope.

Does this achieve the same effect? Or should I just stick to using raw pointers for the widgets?

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 352
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Memory Management of a panel using unique_ptr

Post by New Pagodi » Sun Jun 07, 2020 4:19 am

Just use raw pointers. The panel is owned by the frame and so it will be deleted when the frame is destroyed. So there is no possibility of leaking the panel object.

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

Re: Memory Management of a panel using unique_ptr

Post by ONEEYEMAN » Sun Jun 07, 2020 5:42 am

Hi,
Did You get any memory leaks by using the raw pointers?

Thank you.

Post Reply