Your problem is correct, the reference count must be moved to a local variable before the object is deleted.
STDMETHODIMP_ (ULONG) ComCar::Release()
{
ULONG refCount = --m_refCount;
if(refcount==0) delete this;
return refCount;
}
But even this code is still erroneous because it is not thread protected.
you should use code like this instead.
STDMETHODIMP_ (ULONG) ComCar::Release()
{
LONG cRefs = InterlockedDecrement((LONG*)&m_refCount);
if (0 == cRefs) delete this;
return (ULONG)max(cRefs, 0);
}
source
share