Results 1 to 20 of 23

Thread: [Memory managament] long liveliness of application

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Memory managament] long liveliness of application

    I write big application which will have to run 24/7, without turning it on and off. It has many dialogs, popups and stuff (it is application to manage production). My problem is that i have noticed after dialog pops up and someone close it, memory doesn't return to state before event occured. I mean, if application used 14000 KB of memory before dialog, it pops up, close and application uses 14100KB (even if dialog doesn't store any data). I read that all widgets used in my widget have to have parent set, so when my widget is deleting, all child widgets will be also deleted, but i have done this condition. How can i be sure that i release all memory?

  2. #2
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    It's hard to blind guess such a leak, mainly because it's usually something one tends to overlook because of it's level of obviousness. Off the top of my hat, when I started I used to not call the QWidget constructor in my custom QWidget constructor.

    Also, are you sure you are not just instanciating a new widget every time you open it?

    if you do something like this:
    Qt Code:
    1. QPushButton * qpbOfFunAndJoyAndHappyness = new QPushButton("Push Button");
    2. qpbOfFunAndJoyAndHappyness->show();
    To copy to clipboard, switch view to plain text mode 

    every time you want to show a button (just to give an example, I am not saying this is your case), this will always generate a new instance of it, which when you close you lose direct control of and which will only be deleted when you delete the instance of whatever it is that's calling those two lines of code. (and even then, only if parenting is done properly)

    Again, this is just from my experience. Common errors I used to make all the time (and that even now I do every now and then, specially when I am too obsesed with deploying a prototype and not minding memory performance).

    My best advice is to check step by step what's happening with the widgets. Try to pinpoint which one is the one that's leaking and then track it.

    Sorry I couldn't be of much help, but it's really hard without something to work on.

  3. #3
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    Yea, of course, im stupid. I forgot to show you how i'm doing it. This is just a template of how i invoke my dialogs:

    Qt Code:
    1. /*somewhere in main window constructor*/
    2. button = new QPushButton("Show dialog", this);
    3.  
    4. connect(button, SIGNAL(clicked()), this, SLOT(showDialog()));
    5.  
    6. /*...*/
    7.  
    8. void myMainWindow::showDialog()
    9. {
    10. dialog dial(this);
    11.  
    12. dial.setGeometry(100,100,200,200);
    13. dial.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp:

    Qt Code:
    1. dialog::dialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. label = new QLabel("Seomthing", this);
    5.  
    6. edit = new QLineEdit(this);
    7.  
    8. button = new QPushButton("My Button", this);
    9.  
    10. lay = new QVBoxLayout(this);
    11. lay->addWidget(label);
    12. lay->addWidget(edit);
    13. lay->addWidget(button);
    14.  
    15. setLayout(lay);
    16. }
    To copy to clipboard, switch view to plain text mode 

    That's how it looks like but much, much simpler than it is in real project. And what i think is that dial will be created in showDialog() function, but when we exit from this function, dial object should be deleted (which outcome from C++ variables locality) along with all its child widgets. Am i right?

  4. #4
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    Isn't .exec() a slot? why not just instance your dialog widget in your main window constructor and connect your click() signal to the slot exec()?

    Also, if your code happens to work as it is right now, it wouldn't surprise me that it might be leaking. Think about it; you assume that when going out of scope of the showDialog() method it will delete local stuff, which is right, but then you exec something that, from what I understood, does not close the exact same moment the showDialog() method finished, hence it did not get deleted (or it would either close the dialog or crash).

    Another option that I think would work for your code as it is right now, is to set the attribute Qt::WA_DeleteOnClose to your dial instance.

    Qt Code:
    1. void myMainWindow::showDialog()
    2. {
    3. dialog dial(this);
    4. dial->setAttribute(Qt::WA_DeleteOnClose);
    5. dial.setGeometry(100,100,200,200);
    6. dial.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    if that works, then what I said earlier is true. Your assumption about the object being deleted is wrong (can't tell why, though) and that is clear because the dialog itself is not insta-closing.

  5. #5
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    I have to do it this way, because after dial.exec() i'm doing some more stuff, depends on if dialog was accepted or rejected.

    from what I understood, does not close the exact same moment the showDialog() method finished, hence it did not get deleted (or it would either close the dialog or crash)
    Actually i did some experimets. I wrote my own QLabel, i mean class which inherits from QLabel and which shows up message box when destructor is called.

    Qt Code:
    1. class myLabel : public QLabel
    2. {
    3. Q_OBJECT
    4. public:
    5. myLabel(const QString & text, QWidget * parent = 0) : QLabel(text,parent) {};
    6. ~myLabel() { QMessageBox::information(this, "asd", "label destructor"); };
    7.  
    8. };
    To copy to clipboard, switch view to plain text mode 

    And in dialog.cpp

    Qt Code:
    1. dialog::dialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. label = new myLabel("Seomthing", this);
    5.  
    6. edit = new QLineEdit(this);
    7.  
    8. button = new QPushButton("My Button", this);
    9.  
    10. lay = new QVBoxLayout;
    11. lay->addWidget(label);
    12. lay->addWidget(edit);
    13. lay->addWidget(button);
    14.  
    15. setLayout(lay);
    16. }
    17.  
    18. dialog::~dialog()
    19. {
    20. QMessageBox::information(this, "asd", "Dialog destructor");
    21. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. void test::showDialog()
    2. {
    3. dialog dial;
    4.  
    5. dial.setGeometry(100,100,200,200);
    6. dial.exec();
    7.  
    8. QMessageBox::information(this, "asd", "Function");
    9. }
    To copy to clipboard, switch view to plain text mode 

    And what i'm getting in result of this program (after showing dialog) is:

    Function
    Dialog destructor
    label destructor

    So it means that destructors are called fine, but still in this simple example. Memory usage before dialog: 2160K, during dialog is opened: 2668K, after dialog closed: 2408K (i did measurs without messageboxes)

    EDIT

    If i add this flag, program is crashing after window closes.
    Last edited by Blood9999; 9th August 2012 at 22:08.

  6. #6
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    Have you tried the deleteOnClose flag?

  7. #7
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    Yes i did.

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Memory managament] long liveliness of application

    Quote Originally Posted by alitoh View Post
    Think about it; you assume that when going out of scope of the showDialog() method it will delete local stuff, which is right, but then you exec something that, from what I understood, does not close the exact same moment the showDialog() method finished, hence it did not get deleted (or it would either close the dialog or crash).
    sorry, but 'exec'ing something' does not break the rules of c++ which is that all stack instances are destructed on scope exit. So if dialogs are on the stack, they will dtor when they leave scope. In this case, WA_DeleteonClose is redundant - it is only needed for widgets on the heap.


    it should be pointed out that the op is judging a memory leak from o/s memory tools which is definitely not a suitable tool for such a job and can often be misleading.

    So it means that destructors are called fine, but still in this simple example. Memory usage before dialog: 2160K, during dialog is opened: 2668K, after dialog closed: 2408K (i did measurs without messageboxes)
    This is worthless information as I explain above.

    WA_DeleteOnClose flag appears to cause 'double deletion' for widgets on the stack => crash.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #9
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    Quote Originally Posted by amleto View Post
    sorry, but 'exec'ing something' does not break the rules of c++ which is that all stack instances are destructed on scope exit. So if dialogs are on the stack, they will dtor when they leave scope. In this case, WA_DeleteonClose is redundant - it is only needed for widgets on the heap.
    One second thought, should he be calling the QWidget destructor as he did with the constructor? His redefinition might not be calling it, only printing some misleading debug messages.

    I am surprised at how something can be deleted from either the heap or the stack, and still apparently work. I just tried doing that with a QWidget in a simple qt4 app and it just "closes" itself (destructs) on scope exit.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [Memory managament] long liveliness of application

    Quote Originally Posted by alitoh View Post
    One second thought, should he be calling the QWidget destructor as he did with the constructor? His redefinition might not be calling it, only printing some misleading debug messages.
    Destructors are invoked automatically when the object lifetime ends (i.e. out-of-scope for stack objects, delete for heap objects), you don't call them directly. (Actually exceptions thrown in certain ways can cause destructors to be skipped but your program is terminating horribly anyway)

    I am surprised at how something can be deleted from either the heap or the stack, and still apparently work. I just tried doing that with a QWidget in a simple qt4 app and it just "closes" itself (destructs) on scope exit.
    I have no idea what you mean by "still apparently work." If an object is destroyed it cannot work. When a stack-based object goes out of scope it is destroyed: no ifs, no buts, just destroyed. An object on the heap persists and continues to exist and work until something in the code causes delete to be called on it; then it is destroyed. This is plain C++.

  11. #11
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    Quote Originally Posted by ChrisW67 View Post
    I have no idea what you mean by "still apparently work." If an object is destroyed it cannot work. When a stack-based object goes out of scope it is destroyed: no ifs, no buts, just destroyed. An object on the heap persists and continues to exist and work until something in the code causes delete to be called on it; then it is destroyed. This is plain C++.
    Read the OP. He is not using heap memory, he is using stack-based objects *and* at the same time they don't seem to be getting terminated when going out of scope. That's been exactly my point this whole time. Also, misleading or not, his is an application that runs 24/7, and he clearly stated that he has noticed a constant increase in memory consumption. That's completely aside from whichever method for measuring of memory consumption you are using. It's a visible fact and should be addressed. Is it the best diagnosis? No. Is it a wrong diagnosis? No. If the same process keeps making the application's memory consumption to constantly escalate, there is clearly a leak. That is plain C/C++ style memory management.

    I am not questioning the C++ behaviour, poindexter, I am saying that as far as I understood his problem, his dialog window is persisting, regardless of the scope the application is currently in, which in turn seems to suggest something is not getting deleted. Otherwise his problem would be "my dialog appears and disappears" or "dialog closes up", which are entirely different issues than what's being currently described as the problem. I am not suggesting this is the way C++ or Qt works. There's a big difference.

    He executes showDialog(), which in turn instantiates and executes a dialog, this thread moves on, OUT of the showDialog() scope into whatever else there is in the callstack, but the dialog is still showing/working. This is NOT expected behaviour for the currently provided code because, as you said, it is not C++ behaviour.

    The way he wrote his application, when showDialog() instantiates the dialog and executes it, it "appears" on-screen, but as soon as that method is over, the dialog should close itself because of scope variables deletion. This doesn't seem to be the case. What seems to be the current behaviour is what I mentioned before.

    Blood9999: Can you define what's currently happening? Is your dialog closing when going out of the showDialog()'s scope? Or does it persist?
    Last edited by alitoh; 10th August 2012 at 05:05.

  12. #12
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    From user point of view everything works perfectly fine. After click on button, new dialog appers. When i close it, function ends and destroys dial object ( i checked that via messageboxes in destructors) so memory should get back to earlier 'state', but it doesn't. Weird stuff...

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

    Default Re: [Memory managament] long liveliness of application

    Quote Originally Posted by alitoh View Post
    He executes showDialog(), which in turn instantiates and executes a dialog, this thread moves on, OUT of the showDialog() scope into whatever else there is in the callstack, but the dialog is still showing/working.

    The way he wrote his application, when showDialog() instantiates the dialog and executes it, it "appears" on-screen, but as soon as that method is over, the dialog should close itself because of scope variables deletion. This doesn't seem to be the case. What seems to be the current behaviour is what I mentioned before.
    I am sorry but I cannot see how you can infer that from any of the OP's posts. Quite the contrary actually: I understood that the dialogs disappeared as expected. The OP's problem was the constantly increasing memory usage, which, as already explained above, cannot reliably be related to a memory leak.

    @Blood9999
    There might be a memory leak in another part of your program. Have you tried to run a tool such as valgrind?

    EDIT:
    @amleto: sorry, I hadn't refreshed to see your last post.

  14. #14
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    @yeye_olive
    I write it on Windows, because it has to work on this system, so i suppose i would need to use another tool. I will have to use something because it is driving me crazy. Also i write it in Visual Studio 2010 Ultimate, so maybe is there any tool to do that?

  15. #15
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Memory managament] long liveliness of application

    I think this is what I used at a former job http://www.softwareverify.com/cpp-memory.php free 30 day trial iirc


    I've just come across gperftools as well (google performance)
    http://code.google.com/p/gperftools/?redir=1
    Last edited by amleto; 10th August 2012 at 14:28.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    Blood9999 (10th August 2012)

Similar Threads

  1. QSqlDatabase best practices with long-running application
    By redBeard in forum Qt Programming
    Replies: 6
    Last Post: 17th October 2011, 19:32
  2. Replies: 2
    Last Post: 7th September 2011, 13:12
  3. Problem: the Application Takes very long time to build
    By Ma7moud El-Naggar in forum Qt Programming
    Replies: 5
    Last Post: 20th November 2010, 06:26
  4. Replies: 2
    Last Post: 5th October 2010, 08:20
  5. Replies: 3
    Last Post: 6th January 2010, 16:55

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.