Results 1 to 6 of 6

Thread: Widget Memory Allocation

  1. #1
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Widget Memory Allocation

    Greetings. This is more of a C++ question, but I'm posting it here because I would like to know how it relates to Qt.

    Reading books on Qt, I know that widgets should be constructed on the heap because they will be deleted automatically when their parents are deleted. My question is this: if I have a custom widget class and I instantiate it on the heap, would the members also go on the heap even though they are not dynamically created in the class?

    Qt Code:
    1. class MyWidget : public QWidget{
    2.  
    3. private:
    4. QPushButton myButton_;
    5. };
    6.  
    7. ...
    8.  
    9. MyWidget *w = new MyWidget;
    To copy to clipboard, switch view to plain text mode 

    In the example code does 'myButton_' reside on the heap or the stack? Printing the memory address it seems that it's on the heap. Does this mean that I can avoid using the 'new' operator and dynamic memory allocation for members of my class if I know for sure that the class will be constructed on the heap?

    or is it always best to use pointers regardless?
    Qt Code:
    1. class MyWidget : public QWidget{
    2.  
    3. public:
    4. MyWidget(){
    5. pMyButton_ = new QPushButton;
    6. }
    7.  
    8. ~MyWidget(){
    9. delete pMyButton_;
    10. }
    11. private:
    12. QPushButton *pMyButton_;
    13. };
    14.  
    15. ...
    16.  
    17. MyWidget *w = new MyWidget;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Widget Memory Allocation

    Quote Originally Posted by ArlexBee-871RBO View Post
    My question is this: if I have a custom widget class and I instantiate it on the heap, would the members also go on the heap even though they are not dynamically created in the class?
    Yes.

    In the example code does 'myButton_' reside on the heap or the stack?
    It resides on the heap.
    Does this mean that I can avoid using the 'new' operator and dynamic memory allocation for members of my class if I know for sure that the class will be constructed on the heap?
    No. It doesn't matter whether an object resides on the heap or on the stack (you can use "new" to allocate an object in stack space as well using so called "placement new"). What matters is whether the object will be automatically destroyed or not when its "parent scope" ends. For class members their parent scope is the lifetime of the object they are contained in.

    or is it always best to use pointers regardless?
    In this situation you have to use a pointer for the button because otherwise it's prone to double-deletion.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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: Widget Memory Allocation

    Your pushbutton is also created on the heap. The only difference to private pointers is, that the first version needs a bigger continious space on the heap. Anyway I would suggest you to use the second variant with the pointers but also note, that your delete statement is not necessary: Just pass "this" to the c-tor of your QPushButton and it will be deleted by Qt!

    Edit: too late...

  4. #4
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Re: Widget Memory Allocation

    Thanks for the quick reply. I do pass a parent pointer to my members, so it's all good.

    For non-Qt, general C++, many have suggested that memory should be allocated on the stack whenever possible, and allocated on the heap when there is no other choice. I'm not sure if I agree with that; allocation on the stack seems to be faster, but based on my experience there is a massive performance hit if your member data are misaligned. Also, I always have the fear of encountering stack overflow.

    so, what about non-widget Qt classes that don't accept parent pointers? If I have a class with many QPixmap and QBrush objects as members, is it also best to put them on the heap?

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Widget Memory Allocation

    It depends on the situation. If a class can be deleted by someone who isn't the owner (eg. Widgets where the parent widget can delete them for you), then you are best with heap based storage to ensure that you don't try and delete an object which may have been already deleted.

    For a 'normal' C++ class, then I prefer stack-based allocation for small objects, and heap based for objects known to be quite large. Stack based allocation is fast (one subtract on a processor register and the allocation is done), and ensures the object is deleted when it goes out of scope.

    You might have guessed that I don't want the responsibility to delete objects if someone else can delete them for me.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Widget Memory Allocation

    Guys, please let's not abuse the terms "heap" and "stack". Let's call that "dynamic" and "static" allocation. If you have an object that is allocated on the heap and it has member variables (but not pointers), they will also be allocated on the heap. Also let's remember most Qt objects use the p-impl idiom, so even if you allocate such object "on the stack", most of it will actually still end up on the heap. There is no magic happening with the words "stack" and "heap" apart from that on some architectures stack memory is very limited so most objects should be allocated in the dynamic memory space (heap or global).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Changing memory allocation managment ( QT 4.4.3 )
    By Link130 in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th October 2009, 10:44
  2. Virtual memory allocation problem
    By morfei in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2009, 12:30
  3. QDrag : memory allocation
    By kghose in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2008, 23:57
  4. limit memory allocation
    By magland in forum General Programming
    Replies: 10
    Last Post: 23rd March 2007, 10:21
  5. vector memory allocation
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 23rd March 2006, 18:27

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.