I am currently studying COM and the following code confused me.
STDMETHODIMP _(ULONG) ComCar::Release()
{
if(--m_refCount==0)
delete this; // how could this "suicide" deletion be possible?
return m_refCount;
}
I am wondering how could it be possible to delete an object instance within its member method? So I made the following experiment:
class A
{
public:
void Suicide(void);
void Echo(void);
char name;
};
void A::Echo(void)
{
::printf("echo = %c\n",name);
}
void A::Suicide(void)
{
delete this;
}
int main(void)
{
A a;
a.name='a';
a.Suicide(); //failed
}
And the execution does failed at a.Suicide(). The debug report some "Debug Assertion Failed". Could someone shed some light on me? Cause I am totally a newbie on COM.
A related thread is here: http://stackoverflow.com/questions/2308657/question-about-com-release-method