I am learning about COM and reading about this code:
STDMETHODIMP_ (ULONG) ComCar::Release()
{
if(--m_refCount==0) delete this;
return m_refCount;
}
My question is, if the m_refCount==0 and the object is deleted, how could the instance member variable m_refCount still exist and be returned? Please forgive me if my question is so naive cause I am a totally newbie on COM. Many thanks.
A related thread is here: How could a member method delete the object?
Your concern is valid, the ref count should be moved into a local variable before the object is deleted.
But even that code is still wrong because it’s not thread safe.
you should use code like this instead.