Results 1 to 11 of 11

Thread: Free memory

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    15

    Default Free memory

    Hi,

    I'm creating a form by:
    Qt Code:
    1. FormPusty::FormPusty(QWidget *parent) : QMainWindow(parent)
    2. {
    3.  
    4. base = new QWidget();
    5. QGridLayout *layout = new QGridLayout(base);
    6. QTabWidget *TwNew = new QTabWidget();
    7. QWidget *page1 = new QWidget();
    8. TwNew->addTab(page1,QIcon::QIcon(":/new/prefix1/doc_stand.png"),"pierwsze");
    9.  
    10. layout->addWidget(TwNew,0,0,1,1);
    11. base->show();
    12. this->setCentralWidget(base);
    13. this->move(((QApplication::desktop()->width()-this->width())/2),((QApplication::desktop()->height()-this->height())/2));
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    and in application:
    Qt Code:
    1. OFormPusty = new FormPusty();
    2. OFormPusty->show();
    To copy to clipboard, switch view to plain text mode 

    and I've discovered there is no free memory in my app :/ How to add a free memory, I've a several forms whith alocated almost 10Mb :/ when I'm closing a form memeory is still use :/
    ofcourse I've :

    Qt Code:
    1. FormPusty::~FormPusty(){
    2. qDebug()<<"ttt";
    3. delete base ;
    4. }
    To copy to clipboard, switch view to plain text mode 

    but is noting happend :/

  2. #2
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanked 11 Times in 11 Posts

    Default Re: Free memory

    This is working as intended. If you show() a dialog after you close it its destructor don't get called and it's there to be used again.
    On the contrary if you exec() a dialog when you close the instant gets destructed. The difference between those 2 function is when you
    show() the dialog is modeless and on exec() it's modal. See more on the documentation about this.
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  3. The following user says thank you to Archimedes for this useful post:

    soulless (14th February 2010)

  4. #3
    Join Date
    Apr 2009
    Posts
    75
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    15

    Default Re: Free memory

    Thanks - you have said that replace show() to exec() solve my problem, unfortunately there's no member function llike exec() in QMainWindow :/

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Free memory

    well to possibilities:
    Have you "OFormPusty = new FormPusty();" in a function which is called multiple times? then you have to use "delete OFormPusty;" or use QObject::deleteLater().
    Or how do you measure the occupied space? Sometimes the memory is freed but still assigned to the app if it need it again. So there must not be a real memory leak.

  6. The following user says thank you to Lykurg for this useful post:

    soulless (14th February 2010)

  7. #5
    Join Date
    Apr 2009
    Posts
    75
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    15

    Default Re: Free memory

    Yes, a I've exactly:
    Qt Code:
    1. void MainWindow::slotFormPusty(){
    2. OFormPusty = new FormPusty ();
    3. connect(OFormPusty , SIGNAL(singalDone()), this, SLOT(slotDone()));
    4.  
    5. OFormPusty ->show();
    6. }
    To copy to clipboard, switch view to plain text mode 
    OFormPusty is a member of MainWindow.

    you said than I should write delete OFormPusty; a in slotDone() ?
    I mesure the occupied space by Windows task manager

  8. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Free memory

    if there should be only one window use
    Qt Code:
    1. void MainWindow::slotFormPusty(){
    2. if(OFormPusty)
    3. OFormPusty->deleteLater();
    4. OFormPusty = new FormPusty ();
    5. connect(OFormPusty , SIGNAL(singalDone()), this, SLOT(slotDone()));
    6.  
    7. OFormPusty ->show();
    8. }
    To copy to clipboard, switch view to plain text mode 
    if you have more instances of that form set the widget attribute Qt::WA_DeleteOnClose to ensure that the memory gets freed after closing the window.


    EDIT: If there is only one instance a time, don't create a new one, just reuse the old one:
    Qt Code:
    1. void MainWindow::slotFormPusty(){
    2. if(!OFormPusty)
    3. OFormPusty = new FormPusty ();
    4.  
    5. connect(OFormPusty , SIGNAL(singalDone()), this, SLOT(slotDone()));
    6. OFormPusty ->show();
    7. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Lykurg for this useful post:

    TomASS (14th February 2010)

  10. #7
    Join Date
    Apr 2009
    Posts
    75
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    15

    Default Re: Free memory

    Thank's Lykurg! You've a right.

  11. #8
    Join Date
    Apr 2009
    Posts
    75
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    15

    Default Re: Free memory

    Unfortunately, I've somthing strange:

    I put:
    Qt Code:
    1. if(OFormPusty){
    2. OFormPusty->deleteLater();
    3. delete OFormPusty;
    4. }
    5. OFormPusty= new OFormPusty();
    To copy to clipboard, switch view to plain text mode 

    Sometimes (50% cases) when I've compiled my app this code throw me an error (debug shows me QWaitCondition: Destroyed while threads are still waiting ) - when I compiled again, it should be good (but not sure) :/

  12. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: Free memory

    OFormPusty->deleteLater(); schedules the deletion in next event loop.
    So you shouldnt use it. This should work fine -
    Qt Code:
    1. if(OFormPusty){
    2. delete OFormPusty;
    3. OFormPusty = 0; // To prevent improper use if any.
    4. }
    5. OFormPusty= new OFormPusty();
    To copy to clipboard, switch view to plain text mode 

    But why so you want to delete and create again ? As Lykurg said, you can do with single object

  13. The following user says thank you to aamer4yu for this useful post:

    TomASS (15th February 2010)

  14. #10
    Join Date
    Apr 2009
    Posts
    75
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    15

    Default Re: Free memory

    Thank's

    I'ld like to create a new window (new OFormPusty), when user push the button (it does not make sense create a form when application is starting). When user close a form and push the button again, I'ld like to show form with new contents so, delete and create a form is fine for me...i think so.

  15. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Free memory

    Quote Originally Posted by TomASS View Post
    When user close a form and push the button again, I'ld like to show form with new contents so, delete and create a form is fine for me...i think so.
    Well it works but it is not fine! use this and try to fully understand what the code is doing:
    Qt Code:
    1. void MainWindow::slotFormPusty(){
    2. // next lines checks if the form exists, because on the initialisation of the class the form is not created!
    3. // so it is only created if the user request it.
    4. if(!OFormPusty)
    5. {
    6. OFormPusty = new FormPusty ();
    7. connect(OFormPusty , SIGNAL(singalDone()), this, SLOT(slotDone()));
    8. // do initial setup here
    9. }
    10. QFormPusty->setToDefault(); // this is a custom slot that should clear al temporary data. So that the form is "new"
    11. OFormPusty ->show();
    12. }
    To copy to clipboard, switch view to plain text mode 
    This is - as far as I know - the best solution for you.

    But as said your way works also.

    Lykurg


    P.s.: the difference is that the above version will require space permanently and your approach only need the space temporary but require always a new creation of the form which costs cpu power.

Similar Threads

  1. free up the memory used by QHash
    By vishal.chauhan in forum Qt Programming
    Replies: 8
    Last Post: 22nd June 2009, 20:13
  2. program doesn't seem to free memory, linux
    By JeanC in forum Qt Programming
    Replies: 5
    Last Post: 1st September 2008, 13:06
  3. What to free or not to free, that is the question
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 06:04
  4. what is free store in C++ memory?
    By Masih in forum General Programming
    Replies: 6
    Last Post: 2nd July 2007, 23:25
  5. Replies: 2
    Last Post: 13th February 2006, 16:42

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.