PDA

View Full Version : will Qt delete all dynamic data?



somename
30th May 2010, 07:41
I have dynamic arrays, dynamic qobject, dynamic qwidget... and I have no one destructor!
Will qt delete it automatically?
Or I need write some destructors?

Lykurg
30th May 2010, 09:14
It depends. If they are childes of some QObject, then they will be deleted by Qt, otherwise you have to take care about them yourself.

Zlatomir
30th May 2010, 10:55
Be extra careful with dynamic arrays, for sure they are not children of some QObject that will delete them, so your in charge for the dynamic allocated data containers.

I recommend to use Qt's or STL containers, they manage their own memory and don't leak.

somename
31st May 2010, 06:04
For example, if I have following class:

class Widget : public QWidget
{
Q_OBJECT
public:
Widget()
{
label = new QLabel;
labelChild = new QLabel(this);
for (int i = 0; i < 10; i++) {
arrayOfButtons[i] = new QPushButton;
arrayOfInt[i] = new int;

QPushButton *button = new QPushButton;
listOfButtons.append(button);
}
}

~Widget()
{
//What i need to delete?
}

private:
QLabel *label;
QLabel *labelChild;

int *arrayOfInt[10];
QPushButton *arrayOfButtons[10];
QList<QPushButton*> listOfButtons;
}


And need i do this:
Widget(QWidget *parent) : QWidget(parent)
{
...
}

And this:

QApplication app(argc, argv);

QMainWindow *mainWidget = new QMainWidget(&app);


and please, If you can, give to me some easy app, that can show to me the memory leaks

graeme
31st May 2010, 07:06
Basically it depends upon how you create the object. It's not just sufficient to have a class inherit from a QObject for it to be managed by Qt. If you create an object and ensure that it has a parent then when the parent is deleted it will delete it's children.

So in the example you gave mainWidget has the parent app, so when the application is closed then mainWidget will be freed. labelChild has the Widget as it's parent so when Widget is deleted it will ensure that labelChild is properly deleted. However, your pushButtons are not assigned any parent and so they will not be tidied up.

You should see that your code as posted needs some modification but the changes are trivial. As a help put a qDebug() statement in your destructors, just so that you understand what is going on and that you are taking advantage of the auto deletion capabilities of Qt

somename
31st May 2010, 07:49
#ifndef WIDGET_H
//widget.h
#define WIDGET_H

#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = 0) : QWidget(parent) {}

~Widget()
{
qDebug() << "I AM IN DESTRUCTOR OF WIDGET!";
}
};

#endif // WIDGET_H


#include "widget.h"

int main(int argc, char **argv)
{
QApplication app(argc, argv);

Widget *widget = new Widget(&app);
widget->show();

return app.exec();
};

this is not work..

Hmm.. how to set parent of Widget using QApplicatoin?

Lykurg
31st May 2010, 08:02
in main create the widget on the stack, then it will be deleted when you leave the scope. Since Qt uses pimpl the main part of the object is allocated dynamically. but if you want to create your main window on the heap use:
int returnValue = app.exec();
delete widget;
return returnValue;

somename
31st May 2010, 08:07
Ok, and please, give me some small utility, that can show to me memory leaks..(for windows)

graeme
31st May 2010, 08:52
Try the following:

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = 0) : QWidget(parent) {}

~Widget()
{
qDebug() << "I AM IN DESTRUCTOR OF WIDGET!";
}
};

#endif // WIDGET_H



#include "widget.h"


int main(int argc, char **argv)
{
QApplication app(argc, argv);

QMainWindow mainWin;
Widget *widget = new Widget(&mainWin);
mainWin.show();

return app.exec();
};

See that whilst in the mainWindow I've not set up a parent when the app closes it gets deleted (because it's on the stack) and then the widget which is a child of mainWindow is deleted. (Or at least the destructor is called!)

somename
31st May 2010, 16:49
Ok, and one more question.
I have mainWidget, that contain several other widget (window),
and when I do following code, on my mainWidget I see other widget... I don't want that it show on mainWidget..


class MainWidget : public QMainWidget
{
Q_OBJECT
public:
MainWidget()
{
myForm = new MyForm(this);
}

private:
MyForm *myForm;

}

How to do myForm as a child without showing on MainWidget?

Lykurg
31st May 2010, 18:33
where do you want to show it?
Inside your main window: use layouts.
outside your main widnow: use QDialog as a base class for example. or set the modal widget attribute.

somename
31st May 2010, 18:42
aa.. QDialog..
Ok, thanks!

somename
31st May 2010, 19:57
hmm.. I have one problem.
My app has one QMainWindow, and several other QWidget that can be show on QMainWindow(one at the time, and it is not QTabWidget!!).

There are several situations:

class Widget : public QWidget
{
Widget(QWidget *parent) : QWidget(parent) {};
}
//In this situation, on my QMainWindow are showing several widget at the time


class Widget : public QDialog
{
Widget(QWidget *parent) : QDialog(parent) {};
}
//In this situation, widget show as a dialog, on other window (not in QMainWindow)

class Widget : public QWidget
{
Widget() {};
}
//In this situation all works fine, only this widget are showing on QMainWindow at the time. But it brings memory leaks,
// because this widget has no parent, and i need to delete it by it self..

//In my QMainWindow I do this:
setCurrentWidget(widget)

Lykurg
1st June 2010, 00:18
Have a look at QStackedWidget!