February 28, 2012

shahid.pk shahid.pk
Lab Rat
29 posts

memory leakage and memory management

 

I would like devs here to suggest me topics that i should read for learning Qt’s memory management mechanism.And look at this aap given below,will this aap delete the reserved memory for mainWindow when no reference is left or it will not do it automatically.If yes why?,if no again why?

  1. #include <QtGui>
  2. #include <QApplication>
  3.  
  4. int main (int argc,char *argv[])
  5. {
  6.  
  7.    QApplication qaap(argc,argv);
  8.  
  9.    QMainWindow *mainWindow = new QMainWindow;
  10.  
  11.    mainWindow->show();
  12.  
  13.    return qaap.exec();
  14.  
  15. }

 Signature 

Love to learn….

3 replies

February 28, 2012

mlong mlong
Robot Herder
1551 posts

As the application ends, it will release the memory which was allocated in line 9. But that’s just because it’s the normal cleanup that an application does when it exits. Nothing Qt-specific.

You could just as easily do a standard C++ new like:

  1. int *foo = new int[1000];

and while it would be most proper to delete[] the memory when you were done, if it were still in existence when the app exited, it would be released by the OS as the program ended.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

February 28, 2012

shahid.pk shahid.pk
Lab Rat
29 posts

ok that is clear but i wanted to say that let it be like this if i am using a mainWindow in an aap and doing some other stuff to or you can say i am using the mainWindow in this aap for example

  1. int main (int argc,char *argv[])
  2. {
  3.  
  4.    QApplication qaap(argc,argv);
  5.  
  6.    QMainWindow *mainWindow = new QMainWindow;
  7.  
  8.    mainWindow->show();
  9.    //for example doing some network related stuff with values taken from the mainWindow
  10.   // and main window is no longer needed.so right here what will qt do will it delete the mainWindow
  11.  // pointer itself or not  
  12.    return qaap.exec();
  13.  
  14. }

 Signature 

Love to learn….

February 28, 2012

mlong mlong
Robot Herder
1551 posts

If you delete the main window before qaap.exec() is called, then nothing will ever happen, because the gui doesn’t start up until QAplication::exec() is called.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

 
  ‹‹ [Solved] Can I include extended ascii codes in my application?      Convert QUrl to QString ››

You must log in to post a reply. Not a member yet? Register here!