CoCreateInstance - What is the reverse?

Using COM, you create a CoCreateInstance object .

Is there a way to completely destroy it so that the next part of the unit test can start over?

+3


source to share


1 answer


Anyone who has a pointer to a COM interface is guaranteed that the object is alive, so without knowing everyone who holds the interface pointers to your COM object and being able to request a release, you cannot make sure the object is destroyed.

You can definitely try

for(; ; )
{
  if(pFoo->Release() == 0)
    break;
}

      



However, even so, you (a) can end up with a dead loop, (b) those referencing your object will get an access violation / undefined after they try to access the destroyed object (they still expect the object, referenced by the object).

Another approach is that you use a wrapper object and manipulate the real COM object internally without exposing it. And you are forwarding method calls from the wrapper to the inner object. Then you can ask your wrapper to release internal references and this usually leads to destruction of the internal object.

+6


source







All Articles