Results 1 to 10 of 10

Thread: Qt layout memory issue

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Qt products
    Qt4 Qt5
    Thanks
    98
    Thanked 3 Times in 3 Posts

    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 18:19.

Similar Threads

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