Re: Virtual Destructor - Implication & Specification
On Apr 3, 4:02 am, "Le Chaud Lapin" <jaibudu...@gmail.com> wrote:
There are three ways to solve the heap mismatch problem. You have
pointed out ways #1 and #2. I am going to say what #3 is in a second,
but first #1 and #2:
1. Microsoft KB article Q122675
2. Class-specific new/delete
and now the most important one in context of current dicussion..
3. Simply make the destructor virtual.
In my haste, I made an error. #2 Does _not_ solve the new/delete
mismatch problem. This is actually what my colleagues keey saying,
which I keep refuting. This just goes to show that, if you hear
incorrect information often enough, you start repeating it.
What solves the heap-mismatch problem is the virtual destructor, and
the virtual destructor alone. So #2 should be elminated as a
solution.
Just in case anyone is interested in exploring this issue further, I
have cut-and-pasted some sample code here.
[Note to Mods: I realize this code is platform-specific, but to
illustrate the underlying principle inherently requires platform-
specific code.]
***** FOO.HPP:
struct Abstract
{
virtual ~Abstract (); // Add definition for this in DLL.CPP please.
} ;
struct Concrete
{
~Concrete () {}
} ;
#ifdef DEFINE_EXPORTS
_declspec (dllexport) Abstract * gimme_abstract ();
_declspec (dllexport) Concrete * gimme_concrete ();
#else
_declspec (dllimport) Abstract * gimme_abstract ();
_declspec (dllimport) Concrete * gimme_concrete ();
#endif
***** EXE.CPP
// EXE.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "..\Foo.hpp"
int _tmain(int argc, _TCHAR* argv[])
{
Abstract *pAbstract = gimme_abstract();
Concrete *pConcrete = gimme_concrete();
delete pAbstract;
delete pConcrete;
return 0;
}
***** DLL.CPP
// DLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#define DEFINE_EXPORTS
#include "..\Foo.hpp"
Abstract * gimme_abstract ()
{
return new Abstract;
}
Concrete * gimme_concrete ()
{
return new Concrete;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]