GlinesMome
GlinesMome

Reputation: 1629

Qt & shared_pointer : execution error

I'm try to use tr1's shared_ptr and Qt 4.8.2 but I have some troubles. Here my code :

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <string>
#include <tr1/memory>
using namespace std::tr1;

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTreeView>
#include <QListView>
#include <QWidget>

#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    shared_ptr<QHBoxLayout> mainLayout(new QHBoxLayout);

    shared_ptr<QTreeView> mainFeeds(new QTreeView);

    mainLayout->addWidget(mainFeeds.get());
    shared_ptr<QWidget> mainWidget (new QWidget);
    mainWidget->setLayout(mainLayout.get()); // <--- this line

    shared_ptr<QWidget> rightWidget(new QWidget);
    shared_ptr<QVBoxLayout> rightLayout(new QVBoxLayout);

    shared_ptr<QListView> rightItems(new QListView);
    rightLayout->addWidget(rightItems.get());

    shared_ptr<QListView> rightPreview(new QListView);
    rightLayout->addWidget(rightPreview.get());

    rightWidget->setLayout(rightLayout.get());
    mainLayout->addWidget(rightWidget.get());

    this->setCentralWidget(mainWidget.get());
}

MainWindow::~MainWindow()
{
    delete ui;
}

And the output (I use Qt Creator) :

Starting /path/myproject-build-desktop-Qt_4_8_2_in_PATH_local_Release/myproject... The program has unexpectedly finished. /path/myproject-build-desktop-Qt_4_8_2_in_PATH_local_Release/myproject exited with code 0

When I comment the marked line, the program runs but I have an empty window.

I have two questions :

  1. Why this line makes an error?
  2. Is it the right way to use smart pointers (in fact to build robust C++ code) with Qt?

For your help, In advance, Thanks.

Upvotes: 1

Views: 892

Answers (2)

Lol4t0
Lol4t0

Reputation: 12547

Qt manages objects lifetime in its own manner. When you make A child of B, then A will be delete'd when B destroyed.

So, in this line

mainWidget->setLayout(mainLayout.get());

you make mainLayout child of mainWidget.

As mainWidget declared after mainLayout, it will be removed first. And mainLayout will be removed also. But then shared_ptr will try to remove mainLayout again.

In Qt you should be very carefull using smart pointers. Qt memeory management often take ownership of object, (but not always). Also you may prefer using native Qt smart pointers

Upvotes: 0

Timo Geusch
Timo Geusch

Reputation: 24341

To answer your second question, no, the way you are trying to use shared_ptrs with Qt doesn't work.

Your shared pointers are going out of scope at the end of the function (which destroys the pointed-to object as they are the only and thus last shared_ptr to manage the object's lifetime) while the Qt objects are still clutching raw pointers to objects that have just been deleted. So for that reason alone, your code is not going to work as Qt tries to work with invalid objects. That is undefined behaviour.

Also, Qt does its own resource management. You pass a raw pointer to a child object to its parent and the parent will take care of deleting the children when the parent goes out of scope. So at this point, the destructors of the child objects are being called anyway.

Upvotes: 1

Related Questions