It seems to me that I'm starting to use smart pointers in my qt work. The thing that bothers me is how smart pointers will go with Qt's garbage collection. All Qt stands on the idiom that a child QObject builds with QObject * parent as the ctor argument and therefore allows garbage collection. For instance:
QWidget* mWidget = new QWidget(this);//Here we not only //ensure that mWidget will be deleted //when its parent is deleted, but also tell qt, //that mWidget is not a window, but belongs to //parent layout
Now, if I want to wrap mWidget in a smart pointer.
typedef QScopedPointer<QWidget> WidgPtr; WidgPtr mWidget = WidgPtr(new QWidget(this));
But now, when the parent dtor is called, it will delete twice by the mWidget pointer. First, due to garbage collection, and secondly, when the dtor smart pointer is called.
Of course, we can build mWidget without a parent, and then change some flags to disable window behavior or call setParent () (but then again mWidget will be deleted twice). But for me it is too much to do such a complicated initialization just to be able to use smart pointers instead of raw pointers. Or maybe I missed something? Thanks.
source share