Here are a few simple steps that can help solve the memory leak issue when implementing_dyncreate.
Visual Studio (I’m using 2010, but I’ve tried versions as well) others are telling me that I have a memory leak in the line of my class executing IMPLEMENT_DYNCREATE macros.
Easily create any MFC dialog application.Add grouping derived from CWinThread using ClassWizard:
Class CMyThread: public CWinThread DECLARE_DYNCREATE(CMonThread)Protected: CMyThread(); // Safe constructor used by Strong CreationPublic: virtual BOOL InitInstance(); virtual interval ExitInstance();protected: DECLARE_MESSAGE_MAP();
#include "stdafx.h"#include "TestProject.h"#include "MyThread.h"#ifdef DEBUG_NEW#undef_debug#define THIS_FILEstatic symbol THIS_FILE = __FILE__;#end if// CMyThreadIMPLEMENT_DYNCREATE(CMyThread, CWinThread)CMyThread:: CMyThread()BOOL CMyThread::InitInstance() // DO: Perform thread initialization here returns TRUE;int CMyThread::ExitInstance() // DO: Clean up each thread here Returns CWinThread::ExitInstance();BEGIN_MESSAGE_MAP(CMyThread, CWinThread)END_MESSAGE_MAP()// CMyThread mind handler
Class CThreadAbstract: public CWinThreadPublic: virtual BOOL InitInstance() means 0; mail ExitInstance() = 0;;
And change your real insteadnew class derived from CWinThread on CThreadAbstract:
Class CMyThread: public generic CThreadAbstract DECLARE_DYNCREATE(CMonThread)protected: CMyThread(); // constructor protected by dynamic creationPublic: personal BOOL InitInstance(); a kind of electronic integer ExitInstance();protected: DECLARE_MESSAGE_MAP();
Now instantiate your own thread from your class’s dialog (OnInitDialog) and exit:
CMyThread (empty) pMyThread = (CMyThread *)AfxBeginThread(RUNTIME_CLASS(CMyThread),0,0,CREATE_SUSPENDED);if(pMyThread) pMyThread->m_bAutoDelete = false; pMyThread->ResumeThread();if(pMyThread) pMyThread->PostThreadMessage(WM_QUIT, 0, 0); pMyThread = NULL;
Run the application in the debugger and click OK (or press Esc) to disable it.
Obviously I don’t know exactly what’s going on here with IMPLEMENT_DYNCREATE on this thing. I’ve read the MSDN files and seen where it says the macro is why it creates an instance of the condition class, so I’m assuming this isn’t actually being cleaned up properly, but I have no idea how it’s supposed to be.