Results 1 to 14 of 14

Thread: will Qt delete all dynamic data?

  1. #1
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default will Qt delete all dynamic data?

    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 07:45.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: will Qt delete all dynamic data?

    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.

  3. The following 2 users say thank you to Lykurg for this useful post:

    Zlatomir (30th May 2010), zoz (30th May 2010)

  4. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: will Qt delete all dynamic data?

    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.

  5. The following user says thank you to Zlatomir for this useful post:

    zoz (30th May 2010)

  6. #4
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: will Qt delete all dynamic data?

    For example, if I have following class:

    code Code:
    1. class Widget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. Widget()
    6. {
    7. label = new QLabel;
    8. labelChild = new QLabel(this);
    9. for (int i = 0; i < 10; i++) {
    10. arrayOfButtons[i] = new QPushButton;
    11. arrayOfInt[i] = new int;
    12.  
    13. QPushButton *button = new QPushButton;
    14. listOfButtons.append(button);
    15. }
    16. }
    17.  
    18. ~Widget()
    19. {
    20. //What i need to delete?
    21. }
    22.  
    23. private:
    24. QLabel *label;
    25. QLabel *labelChild;
    26.  
    27. int *arrayOfInt[10];
    28. QPushButton *arrayOfButtons[10];
    29. QList<QPushButton*> listOfButtons;
    30. }
    To copy to clipboard, switch view to plain text mode 

    And need i do this:
    code Code:
    1. Widget(QWidget *parent) : QWidget(parent)
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

    And this:

    code Code:
    1. QApplication app(argc, argv);
    2.  
    3. 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 06:58.

  7. #5
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: will Qt delete all dynamic data?

    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

  8. The following 2 users say thank you to graeme for this useful post:

    giowck (31st May 2010), somename (31st May 2010)

  9. #6
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: will Qt delete all dynamic data?

    code Code:
    1. #ifndef WIDGET_H
    2. //widget.h
    3. #define WIDGET_H
    4.  
    5. #include <QtGui>
    6.  
    7. class Widget : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Widget(QWidget *parent = 0) : QWidget(parent) {}
    13.  
    14. ~Widget()
    15. {
    16. qDebug() << "I AM IN DESTRUCTOR OF WIDGET!";
    17. }
    18. };
    19.  
    20. #endif // WIDGET_H
    21.  
    22.  
    23. #include "widget.h"
    24.  
    25. int main(int argc, char **argv)
    26. {
    27. QApplication app(argc, argv);
    28.  
    29. Widget *widget = new Widget(&app);
    30. widget->show();
    31.  
    32. return app.exec();
    33. };
    To copy to clipboard, switch view to plain text mode 

    this is not work..

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

  10. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: will Qt delete all dynamic data?

    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:
    1. int returnValue = app.exec();
    2. delete widget;
    3. return returnValue;
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to Lykurg for this useful post:

    somename (31st May 2010)

  12. #8
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: will Qt delete all dynamic data?

    Ok, and please, give me some small utility, that can show to me memory leaks..(for windows)

  13. #9
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: will Qt delete all dynamic data?

    Try the following:
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QtGui>
    5.  
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Widget(QWidget *parent = 0) : QWidget(parent) {}
    12.  
    13. ~Widget()
    14. {
    15. qDebug() << "I AM IN DESTRUCTOR OF WIDGET!";
    16. }
    17. };
    18.  
    19. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "widget.h"
    2.  
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QMainWindow mainWin;
    9. Widget *widget = new Widget(&mainWin);
    10. mainWin.show();
    11.  
    12. return app.exec();
    13. };
    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 08:59.

  14. #10
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: will Qt delete all dynamic data?

    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:
    1. class MainWidget : public QMainWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. MainWidget()
    6. {
    7. myForm = new MyForm(this);
    8. }
    9.  
    10. private:
    11. MyForm *myForm;
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

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

  15. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: will Qt delete all dynamic data?

    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.

  16. The following user says thank you to Lykurg for this useful post:

    somename (31st May 2010)

  17. #12
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: will Qt delete all dynamic data?

    aa.. QDialog..
    Ok, thanks!

  18. #13
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: will Qt delete all dynamic data?

    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:
    1. class Widget : public QWidget
    2. {
    3. Widget(QWidget *parent) : QWidget(parent) {};
    4. }
    5. //In this situation, on my QMainWindow are showing several widget at the time
    6.  
    7.  
    8. class Widget : public QDialog
    9. {
    10. Widget(QWidget *parent) : QDialog(parent) {};
    11. }
    12. //In this situation, widget show as a dialog, on other window (not in QMainWindow)
    13.  
    14. class Widget : public QWidget
    15. {
    16. Widget() {};
    17. }
    18. //In this situation all works fine, only this widget are showing on QMainWindow at the time. But it brings memory leaks,
    19. // because this widget has no parent, and i need to delete it by it self..
    20.  
    21. //In my QMainWindow I do this:
    22. setCurrentWidget(widget)
    To copy to clipboard, switch view to plain text mode 
    Last edited by somename; 31st May 2010 at 20:03.

  19. #14
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: will Qt delete all dynamic data?

    Have a look at QStackedWidget!

  20. The following user says thank you to Lykurg for this useful post:

    somename (1st June 2010)

Similar Threads

  1. delete first row of data from .csv file?
    By babymonsta in forum Qt Programming
    Replies: 3
    Last Post: 20th May 2010, 04:39
  2. Replies: 4
    Last Post: 16th January 2010, 11:08
  3. Replies: 4
    Last Post: 4th September 2009, 09:33
  4. Replies: 4
    Last Post: 19th February 2009, 12:10
  5. Dynamic Data Display with OpenGL
    By showhand in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2006, 02:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.