Do I have to deallocate the memory after mfc application get closed by user? [closed]

StackOverflow https://stackoverflow.com/questions/23481082

  •  16-07-2023
  •  | 
  •  

when closing an mfc application by the user, will it deallocate its memory Automatically?

Here is an example.

myapplication.h:

class myapplication: public CDialog
{
//declaring an mfc application

public:
bool isopen();// a flag which tells if the application is opened
}

mainclass.h:

#include myapplication.h
class mainclass
{
private:
myapplication* pmyapp;
public:
int openmyapp();
}

mainclass.cpp:

int mainclass::opendialog()
{

   if(!pmyapp->isopen()) // this means the application was opened then closed by the user, this seems to work fine
   {
     // this seems to be important to make sure of a proper closing and deallocation of the memory
     delete pmyapp;
     pmyapp = NULL;

   }
   pmyapp = new myapplication();
   pmyapp->Create(myapplication::IDD);
   pmyapp->ShowWindow(SW_SHOW);
}

So, if I open the dialog several times I run into DebugBreak()...

有帮助吗?

解决方案

If your application is running after closing that dialog, yes you must destruct objects.

If the application is finishing, you should destruct them too. However any modern OS will clean up the whole allocated memory.

其他提示

Your operating system will (probably) free any memory you neglect to. But it's still a bad idea, because if you don't release memory you allocate and your application runs for a long time, sooner or later you'll run out. And even if it's just one-time allocations we're talking about, if you get used to ignoring a bunch of memory leak messages at shutdown, you'll miss ones that matter more.

Further, as mentioned in the comments, if you don't delete class objects, then even though the OS will ultimately free the memory they occupy, their destructors will never run.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top