Results 1 to 10 of 10

Thread: Qt layout memory issue

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt layout memory issue

    Anyway, as already said by others, don't mix stack and heap variables like that! It's very dangerous!

    Sample of crashing code:
    Qt Code:
    1. QLineEdit textWidget("Salut");
    2. QHBoxLayout * layout = new QHBoxLayout();
    3. layout->addWidget(&textWidget);
    4. QGroupBox * groupBox = new QGroupBox();
    5. groupBox->setLayout(layout);
    6. qDebug() << textWidget.parent(); // => groupBox
    7. delete groupBox;
    To copy to clipboard, switch view to plain text mode 
    Just before the crash, you see that textWidget.parent() is groupBox, so it's destructor deletes textWidget too, but textWidget is on the stack and the application crashes.

    Warning that also a wrong delete can give the same result, also if everything is in the heap!
    Sample of crashing code:
    Qt Code:
    1. QLineEdit * textWidget = new QLineEdit("Salut");
    2. QHBoxLayout * layout = new QHBoxLayout();
    3. layout->addWidget(textWidget);
    4. QGroupBox * groupBox = new QGroupBox();
    5. groupBox->setLayout(layout);
    6. qDebug() << textWidget->parent(); // => groupBox
    7. delete groupBox;
    8. delete textWidget;
    To copy to clipboard, switch view to plain text mode 

    This code, instead, is not crashing!
    Qt Code:
    1. QLineEdit * textWidget = new QLineEdit("Salut");
    2. QHBoxLayout * layout = new QHBoxLayout();
    3. layout->addWidget(textWidget);
    4. qDebug() << textWidget->parent(); // => NULL
    5. QGroupBox * groupBox = new QGroupBox();
    6. groupBox->setLayout(layout);
    7. qDebug() << textWidget->parent(); // => groupBox
    8. layout->removeWidget(textWidget);
    9. qDebug() << textWidget->parent(); // => groupBox
    10. delete groupBox;
    To copy to clipboard, switch view to plain text mode 
    See the comments for the output of qDebug(). Note how QHBoxLayout (add and remove functions) does not change the parent of textWidget.

    In case of doubt, there is a simple rule: use only objects in the heap and store their pointers with a QPointer (it's always better to understand what you are doing, but this may help if you can't find what is crashing or if you have a shared object):
    Qt Code:
    1. QPointer< QLineEdit > textWidget = new QLineEdit("Salut");
    2. QPointer< QHBoxLayout > layout = new QHBoxLayout();
    3. layout->addWidget(textWidget);
    4. QPointer< QGroupBox > groupBox = new QGroupBox();
    5. groupBox->setLayout(layout);
    6. qDebug() << textWidget->parent(); // => groupBox
    7. delete groupBox; // this deletes textWidget and sets the QPointer to NULL
    8. delete textWidget; // textWidget now is NULL so no crash
    To copy to clipboard, switch view to plain text mode 
    A QPointer becomes NULL when it's object is deleted (works only with QObjects).
    And remember you don't need to write code like this:
    Qt Code:
    1. if (ptr != 0) delete ptr;
    To copy to clipboard, switch view to plain text mode 
    because
    Qt Code:
    1. delete 0
    To copy to clipboard, switch view to plain text mode 
    is valid code!

  2. The following user says thank you to iw2nhl for this useful post:

    bunjee (25th August 2007)

  3. #2
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Qt layout memory issue

    Thanks everybody,

    Well well,
    The problem was coming from my side :

    this :

    Qt Code:
    1. ZeTextWidget * textWidget = new ZeTextWidget("Salut");
    2. QHBoxLayout * layout = new QHBoxLayout();
    3. layout->addWidget(textWidget);
    4. delete layout;
    5. delete textWidget;
    To copy to clipboard, switch view to plain text mode 

    and this :

    Qt Code:
    1. ZeTextWidget textWidget("Salut");
    2. QHBoxLayout * layout = new QHBoxLayout();
    3. layout->addWidget(&textWidget);
    4. delete layout;
    To copy to clipboard, switch view to plain text mode 

    Perfectly works.
    When deleted, a layout doesn't delete the assigned objects.

    Concerning the setting the pointer to NULL :

    As marcel said, this doesn't work :

    Qt Code:
    1. void deletePtr(int* x)
    2. {
    3. delete x;
    4. x = NULL;.
    5. }
    6.  
    7. int main(void)
    8. {
    9. int varAllocatedOnStack = 5;
    10. deletePtr(&varAllocatedOnStack);
    11. }
    To copy to clipboard, switch view to plain text mode 

    But I'm more doing something like that :

    Qt Code:
    1. void deletePtr(int* x)
    2. {
    3. delete x;
    4. }
    5.  
    6. int main(void)
    7. {
    8. int * newVar = new int(5);
    9.  
    10. int * newPtr = newVar;
    11.  
    12. (...)
    13.  
    14. newPtr = NULL;.
    15.  
    16. deletePtr(varAllocatedOnStack);
    17. }
    To copy to clipboard, switch view to plain text mode 

    As iw2nhl said setting a pointer to NULL is a valid code.
    Setting a pointer to NULL is a way knowing if a pointer has been assigned to something or not.

    And correct me if I'm wrong but the heap will never create a 0 pointer variable.
    Setting a pointer to NULL is like saying : okay the adress of my pointer isn't valid anymore.

    And I don't see why it would crash anything in my case, and it doesn't.
    Last edited by bunjee; 25th August 2007 at 17:19.

Similar Threads

  1. changing layout of a widget
    By mikro in forum Qt Programming
    Replies: 10
    Last Post: 4th August 2009, 20:21
  2. Qt layout issue
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 15th August 2007, 19:43
  3. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42
  4. Qt4 widget and nested layout issue.
    By bunjee in forum Qt Programming
    Replies: 12
    Last Post: 18th January 2007, 20:29
  5. "dynamic" layout
    By hulk in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2006, 07:16

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
  •  
Qt is a trademark of The Qt Company.