Results 1 to 3 of 3

Thread: Performance comparison of QDialog on stack and heap

  1. #1
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Performance comparison of QDialog on stack and heap

    Hi everyone,

    I read some articles about memory allocation and free in Qt recently. And I have question about the performance of creating QDialogs on stack (non-pointer) vs on heap (new).

    For stack, we just recreate the dialog every time and it should be auto-deleted after we close it, like
    Qt Code:
    1. void parentObjectClass::onButtonClick(){
    2. QDialog dialog(this);
    3. if(dialog.exe()){
    4. // do something
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    I think this one will auto delete the dialog instance every time after the dialog is closed. But every time the dialog need to be re-initialized.

    For heap, we pre-initialize the object and reuse it every time, like

    Qt Code:
    1. // header
    2. Class parentObjectClass{
    3. //....
    4. QDialog *dialog;
    5. //...
    6. };
    7.  
    8. // cpp
    9. void parentObjectClass::initialize(){
    10. dialog = new QDialog(this);
    11. }
    12.  
    13. void parentObjectClass::onButtonClick(){
    14. dialog->setDialog(values);
    15. if(dialog->exe()){
    16. // do something
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    This one just reset the dialog contents every time, but if we have a lot of dialogs, we have to initialize all of them when parentObjectClass instance is created, and they will occupy memory until parentObject is deleted.

    Am I right? And which way is better for practical using?
    Thanks.
    Last edited by Seishin; 22nd August 2012 at 15:54.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Performance comparison of QDialog on stack and heap

    Your question is not really about stack-based vs heap-based allocation, but about creating and destroying dialogs everytime they are show and closed vs reusing them. Indeed, your examples can be rewritten to use the opposite allocation strategy:

    Qt Code:
    1. void parentObjectClass::onButtonClick(){
    2. QDialog *dialog = new QDialog(this);
    3. if(dialog->exec()){
    4. // do something
    5. }
    6. delete dialog;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // header
    2. Class parentObjectClass{
    3. //....
    4. QDialog dialog;
    5. //...
    6. };
    7.  
    8. // cpp
    9. parentObjectClass::parentObjectClass() : dialog(this) {
    10. }
    11.  
    12. void parentObjectClass::onButtonClick(){
    13. dialog.setDialog(values);
    14. if(dialog.exec()){
    15. // do something
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    (note that in the second snippet dialog is not necessarily allocated on the stack, it is just allocated in the same memory block as the parentObjectClass.)

    Now, about your question. I have no strong opinion on the subject. Does it really have a noticeable impact on the performance of your program? One interesting thing to notice is that by reusing a dialog you preserve its state (position and size) while you need to explicitly store it and re-apply it if you use distinct instances.

  3. #3
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Performance comparison of QDialog on stack and heap

    Yes I agree. Usually this won't have a big difference. Actually reusing a dialog make the dialog show immediately after the first showing. This should make user feel the difference. (The dialog looks like switching to a hidden window but not popping up.) And plus it keeps the old status as you said, I do think it should be better to recreate the dialog.

Similar Threads

  1. Allocating widgets on the stack verses using the heap
    By Ronayn in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2011, 00:14
  2. Replies: 3
    Last Post: 12th February 2011, 13:07
  3. Replies: 5
    Last Post: 29th November 2009, 15:09
  4. 2 questions: QAuthenticator and stack / heap
    By Tito Serenti in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2009, 06:56
  5. stack, heap and C#
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th August 2007, 18:40

Tags for this Thread

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.