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?
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?
Last edited by somename; 30th May 2010 at 06:45.
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.
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.
zoz (30th May 2010)
For example, if I have following class:
code Code:
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; }To copy to clipboard, switch view to plain text mode
And need i do this:
code Code:
Widget(QWidget *parent) : QWidget(parent) { ... }To copy to clipboard, switch view to plain text mode
And this:
code Code:
QApplication app(argc, argv); QMainWindow *mainWidget = new QMainWidget(&app);To copy to clipboard, switch view to plain text mode
and please, If you can, give to me some easy app, that can show to me the memory leaks
Last edited by somename; 31st May 2010 at 05:58.
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
code Code:
#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(); };To copy to clipboard, switch view to plain text mode
this is not work..
Hmm.. how to set parent of Widget using QApplicatoin?
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:Qt Code:
int returnValue = app.exec(); delete widget; return returnValue;To copy to clipboard, switch view to plain text mode
somename (31st May 2010)
Ok, and please, give me some small utility, that can show to me memory leaks..(for windows)
Try the following:
Qt Code:
#ifndef WIDGET_H #define WIDGET_H #include <QtGui> { Q_OBJECT public: ~Widget() { qDebug() << "I AM IN DESTRUCTOR OF WIDGET!"; } }; #endif // WIDGET_HTo copy to clipboard, switch view to plain text mode
Qt Code:
#include "widget.h" int main(int argc, char **argv) { QMainWindow mainWin; Widget *widget = new Widget(&mainWin); mainWin.show(); return app.exec(); };To copy to clipboard, switch view to plain text mode
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!)
Last edited by graeme; 31st May 2010 at 07:59.
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..
code Code:
class MainWidget : public QMainWidget { Q_OBJECT public: MainWidget() { myForm = new MyForm(this); } private: MyForm *myForm; }To copy to clipboard, switch view to plain text mode
How to do myForm as a child without showing on MainWidget?
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)
aa.. QDialog..
Ok, thanks!
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:
qt Code:
{ } //In this situation, on my QMainWindow are showing several widget at the time { } //In this situation, widget show as a dialog, on other window (not in QMainWindow) { 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)To copy to clipboard, switch view to plain text mode
Last edited by somename; 31st May 2010 at 19:03.
Have a look at QStackedWidget!
somename (1st June 2010)
Bookmarks