1

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.

1
  • 1
    Use a debugger to trace the execution to the point where the error occurs. Commented Aug 14, 2012 at 20:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

So, if I want to make things good, I have to do not use smart pointers when I deal with Qt elements ?
@GlinesMome, You should not use owner smart pointers, when deal with Qt widgets stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.