Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: [Memory managament] long liveliness of application

  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
    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
    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.
    wrong and wrong.

    The examples shows `dialog.exec();`. This is a blocking call that spins up another event loop. When the event loop exits (ie when the dialog closes), the next line will be executed and eventually control will leave that method's scope and the dialog will be destructed!

    blood, if you truly have a leak, it doesn't come from this scenario. Get a proper memory profiler, even if only a trial version.
    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.

  14. #14
    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.

  15. #15
    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?

  16. #16
    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.

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

    Blood9999 (10th August 2012)

  18. #17
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [Memory managament] long liveliness of application

    As amleto and others have said, using Task Manager as a way to monitor subtle memory use is misleading. You can use it to watch gross memory allocation - going from 200 to 600 MB when some huge file gets read into internal data structure, for example, but for looking at the memory used and freed by a single dialog box, it isn't going to give anything close to an accurate answer.

    Besides, I don't even think that Task Manager is looking at stack-based memory; I think it is probably watching the heap, which your stack-based dialog isn't using.

    So, look elsewhere. What is your dialog doing? Is something inside it allocating heap memory that doesn't get released? Is something that happens after the dialog closes doing it? Is it a one time bump up in memory, or does memory continue to grow every time the dialog gets executed? What happens to memory if you just let the program sit there and run and use other parts of it, but not the dialog?

    Also remember that heap memory gets fragmented as more and more objects are allocated and then freed. Each time you ask for something from the heap, the memory manager has to go find a contiguous chunk big enough to hold it. Depending on how often the memory manager consolidates memory that has been freed, it could find this block in memory that has already been used and discarded or it may have to go out and get some more. If you do a lot of allocation and freeing of objects, your memory footprint may continue to grow even though the sum of all the memory used by the active objects remains relatively constant. This doesn't mean a leak, it just means the memory manager can't keep up with the demand.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  19. The following user says thank you to d_stranz for this useful post:

    alitoh (15th August 2012)

  20. #18
    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 d_stranz View Post
    What is your dialog doing? Is something inside it allocating heap memory that doesn't get released? Is something that happens after the dialog closes doing it? Is it a one time bump up in memory, or does memory continue to grow every time the dialog gets executed? What happens to memory if you just let the program sit there and run and use other parts of it, but not the dialog?
    This. This is the best advice so far to your problem (assuming you pinpointed it to that particular code fragment).

    Quote Originally Posted by amleto
    The examples shows `dialog.exec();`. This is a blocking call that spins up another event loop.
    Are you 100% sure? I could swear I read somewhere that it starts a new thread, but you seem pretty sure about it. If this is the case, I have a serious misconception of it, and I can't find anything on the documentation to confirm or deny it. Please give me a pointer.

  21. #19
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: [Memory managament] long liveliness of application

    If he's using Task Manager, let's say; and he get heavy fragmentation from the awkward placement of objects (again, an assumption) then it's possible that this fragmentation causes task manager to "see" an increasing memory usage because over time larger objects (such as dialogs) have to keep allocating new blocks of space that are big enough; despite all of the memory being released.

    Imagine a scenario (assuming I follow amleto correctly) where using the .exec command delays the destructor:

    Qt Code:
    1. myclass::stuff()
    2. {
    3. QDialog someOtherDialog(this);
    4.  
    5. if(true){
    6. QDialog someDialog(this);
    7. somedialog.exec()
    8. someOtherDialog.exec() //allocated while previous dialog hasn't destructed yet! It's only "rejected" or "accepted"
    9. }
    10. //someOtherDialog is still in scope while someDialog has now destructed
    11. }
    To copy to clipboard, switch view to plain text mode 
    Assuming he's used only contiguous memory to this point the second dialog piles on top of the old one. Assuming they had different scopes as in the above (I may be incorrect; I just thought that I remembered if and loop statements had their own scope) then the next time something is allocated; especially if it's before either dialog has gone out of scope it will allocate new memory without actually being a leak.

    On a side note, use valgrind!

  22. #20
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [Memory managament] long liveliness of application

    Assuming they had different scopes as in the above
    The if clause establishes a different scope (as does the myclass::stuff() method); "someDialog" is allocated when the if clause is entered and deleted when it exits (on the *stack*, not the heap). "someOtherDialog" is allocated when stuff() is entered, remains in scope through the if clause, and is deleted when stuff() exits (also on the *stack* not the heap). This is basic C++, it has nothing to do with Qt and the fact that what is being allocated is QObject-based. These could be any old C++ class or data object; so long as they are allocated on the stack, when they go out of scope, they're gone.

    As far as I know, this does not cause fragmentation, although the stack may have to grow to accommodate the two dialogs. Once each scope is exited, the stack pointer is set back to wherever it was prior to the new scope. The stack is always contiguous memory, as far as I know; the stack pointer just gets moved around.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.