Releasing an Instance

You may have noticed the call to dual.Release in the Create example. The tlb2java superclass IUnknown includes a call to Release in its finalize method. However, for Apartment Model COM objects the finalize method may not be able to call Release because finalizers are normally run from a special thread.

If IUnknown is able to determine a thread Apartment that that owns the interface its finalize method will try to call Release on that thread. Otherwise, you will receive a COMException: Call on wrong thread, if Apartment Thread checking is turned on (see -check_apartment).

It is a good idea to include the Release call in the finally part of a try/finally block.

Dual dual = new Dual();
try {
    useDual(dual);
} finally {
    dual.Release();
}

You can learn more about using Apartment Model Threading with tlb2java here.


$Id: release.html 3769 2007-06-08 19:06:43Z hastings $