c++ - Is there a safe way to use C++11 Smart Pointer and the Interface for Raw Pointer together? -


i want use c++11 smart pointers in new projects, , encounter problem. many current projects still use raw pointers parameters in interface , have no interface smart pointers, e.g. qmainwindow::setcentralwidget.

to keep type consistent, have pass stored pointer get() segment:

qmainwindow win;  std::shared_ptr<qwidget> scrollarea{ std::make_shared<qscrollarea>() }; // qscrollarea derived class of qwidget.  win.setcentralwidget(scrollarea.get()); 

but can't make sure whether other methods in qt execute operator delete on stored pointer of scrollarea.

will cause memory leak or other problems if methods in qt that?

i've checked latest c++ standard cd , found nothing on that. seems it's undefined behavior.

if doing undefined behavior , dangerous, is there safe way use smart pointer(s) interface raw pointer(s)?

there's no such way in general case. each "legacy" interface want use, must read documentation see how interacts ownership (which std smart pointers encapsulate). single object can managed 1 ownership scheme.

with qt in particular, it's not safe mix smart pointers , qt management. qt's parent/child relationship between qobjects includes ownership semantics (children deleted when parent is), cannot safely mix other ownership scheme (such std smart pointers).

note qt docs link explicitly state "qmainwindow takes ownership of widget pointer , deletes @ appropriate time."


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -