Home > Software engineering >  After the MFC CWinAPP class to create a framework, the program out of memory leaks
After the MFC CWinAPP class to create a framework, the program out of memory leaks

Time:09-16

 BOOL CAPPBlockApp: : InitInstance () 
{

INITCOMMONCONTROLSEX InitCtrls;
InitCtrls. DwSize=sizeof (InitCtrls);

InitCtrls. DwICC=ICC_WIN95_CLASSES;
InitCommonControlsEx (& amp; InitCtrls);

CWinApp: : InitInstance ();

EnableTaskbarInteraction (FALSE);

SetRegistryKey (_T (" application wizard to generate local application "));

CMainFrame * pFrame=new CMainFrame;
if (! PFrame)
return FALSE;
M_pMainWnd=pFrame;
PFrame - & gt; LoadFrame (IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
NULL);

PFrame - & gt; ShowWindow (SW_SHOW);
PFrame - & gt; UpdateWindow ();

return TRUE;
}


The problem is CMainFrame * pFrame=new CMainFrame; This way, the final end of the program prompt memory leaks, error point is this way,
But I have been added in the CMainFrame kind PostNcDestroy delete this, and it has executed,
Void CMainFrame: : PostNcDestroy ()
{
Delete this.
}

Excuse me each Daniel, why would an error memory leak? Thank you for advice

CodePudding user response:

PFrame=new CMainFrame; Is a with the original structure CMainFrame (namely pFrame - & gt; LoadFrame (no IDR_MAINFRAME,) Frame

CodePudding user response:

Constructed two MainWnd, delete a, there is another

CodePudding user response:

MFC framework in the window after Destroy the default will be called CFrameWnd: : PostNcDestroy () function, the function will be called the delete this; To delete a pointer, you can don't have to override the virtual function,
If your framework class pointer is not allocated by means of the new words, such as the definition not a class member function, then you need to override the virtual function to modify its implementation, and then call the delete this will certainly lead to abnormal program,

CodePudding user response:

reference 1st floor schlafenhamster response:
pFrame=new CMainFrame; Is a with the original structure CMainFrame (namely pFrame - & gt; LoadFrame (no IDR_MAINFRAME,) the Frame of


But this is automatically generated when VS2010 create project of I, I don't have modified, do you mean, don't need new MainFrame, directly use pFrame - & gt; LoadFrame?

CodePudding user response:

refer to the second floor hhhh63 response:
constructed two MainWnd, delete a and a


Hello, I still don't quite understand, there is only one place new MainFrame ah, don't CWinAPP by default when creating new one?

CodePudding user response:

BigtreeYoung
reference 4 floor response:
Quote: refer to 1st floor schlafenhamster response:

PFrame=new CMainFrame; Is a with the original structure CMainFrame (namely pFrame - & gt; LoadFrame (no IDR_MAINFRAME,) the Frame of


But this is automatically generated when VS2010 create project of I, I don't have modified, do you mean, don't need new MainFrame, directly use pFrame - & gt; LoadFrame?

What I mean is to create a default project should be no memory leak problem, new in the App class InitInstance, PostNcDestroy in will help you invoke the delete this;

CodePudding user response:

refer to 6th floor 7-eleven's response:
Quote: refer to 4th floor bigtreeYoung response:

Quote: refer to 1st floor schlafenhamster response:

PFrame=new CMainFrame; Is a with the original structure CMainFrame (namely pFrame - & gt; LoadFrame (no IDR_MAINFRAME,) the Frame of


But this is automatically generated when VS2010 create project of I, I don't have modified, do you mean, don't need new MainFrame, directly use pFrame - & gt; LoadFrame?

What I mean is to create a default project should be no memory leak problem, new in the App class InitInstance, PostNcDestroy in will help you invoke the delete this;


Boon boon, I have no override PostNcDestroy at the beginning, but the system called the memory leaks, so I'm not very understand 2 or 3 floor, two new MainFrame is what meaning, can't again had new twice?

CodePudding user response:

1 "two new MainFrame" wrong because LoadFrame returns a BOOL
New methods in common use is
//To create the main window, this code creates a new frame window
//object and then sets it as the application 's main window object.
CMainFrame * pFrame=new CMainFrame;
M_pMainWnd=pFrame;
//create and load the frame with its resources
PFrame - & gt; LoadFrame (
IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,
NULL,
NULL
);
2 CWinApp: : InitInstance (); A virtual function, namely CAPPBlockApp: : InitInstance ()
Is his overloading, don't have to call the CWinApp: : InitInstance (); Try to get rid of,
  • Related