Resources that have to be freed.

Resources that have to be freed.

am 25.01.2008 22:45:19 von Gz

Hi All,

I have a class that uses some unmanaged resource through a COM API.
After I finish, I have to explicitly free the resource, or else I will
have resource leaks. The tricky thing is I have to call the COM API
that frees the resource on the same thread as the thread on which I
created the COM component. So freeing the resource in the finalizer is
out of the question, as the GC will call the finalizer on a
differerent thread and cause an exception.

In this case, how do I handle this gracefully? One way is to implement
IDispose and also the finalizer. But in the finalizer I do nothing but
throw an exception so that people won't try to use the code without
explicitly calling Dispose(). Is there any better way?

Thanks,
gz