15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


CMultiDocTemplate objects not being deleted

Kevin McConnell -- kevin@tigger.demon.co.uk
Thursday, June 13, 1996


Platform: VC++ 4.0 / Win 95

I have a MDI app which has 3 document types, and so has three calls to =
AddDocTemplate in InitInstance.  Of these three, two use the same =
document type which does not do any processing (it's all done in the =
views).

My problem is that I have detected memory leaks where the three =
CMultiDocTemplate objects are not being deleted when the app exits.  I =
can't delete them myself since the framework then tries to delete them =
in the CWinApp destructor (which would have suggested they would have =
been deleted?).

Can anyone shed any light on what's going on?  I have MSDN.

The code to add the doc templates is:
	m_pOutBoxTemplate =3D new CMultiDocTemplate(
		IDR_DB1TYPE,
		RUNTIME_CLASS(CDb1Doc),
		RUNTIME_CLASS(CMyChildWnd),       =20
		RUNTIME_CLASS(COutBoxView));
	AddDocTemplate(m_pOutBoxTemplate);

	m_pInBoxTemplate =3D new CMultiDocTemplate(
		IDR_DB1TYPE,
		RUNTIME_CLASS(CDb1Doc),
		RUNTIME_CLASS(CInBoxWnd),       =20
		RUNTIME_CLASS(CInBoxView));
	AddDocTemplate(m_pInBoxTemplate);

	m_pPreviewTemplate =3D new CMultiDocTemplate(
		IDR_PRVTYPE,
		RUNTIME_CLASS(CPreviewerDoc),
		RUNTIME_CLASS(CMDIChildWnd),       =20
		RUNTIME_CLASS(CPreviewerView));
	AddDocTemplate(m_pPreviewTemplate);

The pointers are protected member variables of the CWinApp derived =
class, in order that I can refer to them to open new documents in the =
app.

  Kevin McConnell.



Shiva Shenoy -- shiva@netbuild.com
Friday, June 14, 1996

It was not clear if you tried to delete the doc templates in
ExitInstance() or not.
I ran into the same problem long time back and the following code
solved it.

	if  (m_pOutBoxTemplate)
		delete m_pOutBoxTemplate;
	likewise for the rest of the template pointers....

-Shiva
shiva@netbuild.com

>----------
>From: 	Kevin McConnell[SMTP:kevin@tigger.demon.co.uk]
>Sent: 	Thursday, June 13, 1996 5:29 AM
>To: 	'mfc-l@netcom.com'
>Subject: 	CMultiDocTemplate objects not being deleted
>
>
>Platform: VC++ 4.0 / Win 95
>
>I have a MDI app which has 3 document types, and so has three calls to
>=
>AddDocTemplate in InitInstance.  Of these three, two use the same =
>document type which does not do any processing (it's all done in the =
>views).
>
>My problem is that I have detected memory leaks where the three =
>CMultiDocTemplate objects are not being deleted when the app exits.  I
>=
>can't delete them myself since the framework then tries to delete them
>=
>in the CWinApp destructor (which would have suggested they would have =
>been deleted?).
>
>Can anyone shed any light on what's going on?  I have MSDN.
>
>The code to add the doc templates is:
>	m_pOutBoxTemplate =3D new CMultiDocTemplate(
>		IDR_DB1TYPE,
>		RUNTIME_CLASS(CDb1Doc),
>		RUNTIME_CLASS(CMyChildWnd),       =20
>		RUNTIME_CLASS(COutBoxView));
>	AddDocTemplate(m_pOutBoxTemplate);
>
>	m_pInBoxTemplate =3D new CMultiDocTemplate(
>		IDR_DB1TYPE,
>		RUNTIME_CLASS(CDb1Doc),
>		RUNTIME_CLASS(CInBoxWnd),       =20
>		RUNTIME_CLASS(CInBoxView));
>	AddDocTemplate(m_pInBoxTemplate);
>
>	m_pPreviewTemplate =3D new CMultiDocTemplate(
>		IDR_PRVTYPE,
>		RUNTIME_CLASS(CPreviewerDoc),
>		RUNTIME_CLASS(CMDIChildWnd),       =20
>		RUNTIME_CLASS(CPreviewerView));
>	AddDocTemplate(m_pPreviewTemplate);
>
>The pointers are protected member variables of the CWinApp derived =
>class, in order that I can refer to them to open new documents in the =
>app.
>
>  Kevin McConnell.
>



Niels Ull Jacobsen -- nuj@kruger.dk
Wednesday, June 19, 1996

[Mini-digest: 2 responses]

At 12:29 13-06-96 +-100, you wrote:
>
>Platform: VC++ 4.0 / Win 95
>
>I have a MDI app which has 3 document types, and so has three calls to =3D
>AddDocTemplate in InitInstance.  Of these three, two use the same =3D
>document type which does not do any processing (it's all done in the =3D
>views).
>
>My problem is that I have detected memory leaks where the three =3D
>CMultiDocTemplate objects are not being deleted when the app exits.  I =3D
>can't delete them myself since the framework then tries to delete them =3D
>in the CWinApp destructor (which would have suggested they would have =3D
>been deleted?).
>
>Can anyone shed any light on what's going on?  I have MSDN.

I don't have any specific solutions, but note that a template will only b=
e
deleted=20
in ~CWinApp (actually in ~CDocManager), if the m_bAutoDelete member is tr=
ue.
The=20
interaction of CWinApp, CDocManager, and CDocTemplate is sometimes a litt=
le
unclear,=20
but something which *may* affect you is if you forget to call
CWinApp::InitInstance=20
from your overridden InitInstance.

Also note that the document templates won't be deleted until ~CWinApp - a=
nd
as the application is (normally) a static, they won't be deleted until th=
e
statics=20
are destroyed. This happens *after* you application terminates, so be sur=
e
not to=20
"count" memory leaks until then. CMemoryState can't be used.

Finally, if you can't solve the problem, don't let it spoil your sleep.
Some people won't even think this qualifies as a true memory leak, as the=
=20
objects are allocated only once at the start of the application, lives
thorughout
the application and all memory used by the application will automatically=
 be
freed=20
upon termination anyway.=20

>  Kevin McConnell.
Niels Ull Jacobsen, Kr=FCger A/S (nuj@kruger.dk)
Everything stated herein is THE OFFICIAL POLICY of the entire Kruger=20
group and should be taken as legally binding in every respect.=20
Pigs will grow wings and fly.





-----From: "Dan Opperman" 

Is it possible that your setting CMultiDocTemplate ::m_bAutoDelete to false? 
Doing so would cause the behavior you described.

The following code is from msdev\mfc\src\appui2.cpp:

CDocManager::~CDocManager()
{
	// for cleanup - delete all document templates
	POSITION pos = m_templateList.GetHeadPosition();
	while (pos != NULL)
	{
		POSITION posTemplate = pos;
		CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);

// if you deleted it yourself, the next few lines would do something strange

		if (pTemplate->m_bAutoDelete)
		{
			m_templateList.RemoveAt(posTemplate);
			delete (CDocTemplate*)pTemplate;
		}
	}
}

Hope this helps,
Daniel Opperman




| Вернуться в корень Архива |