Managed class inherit from unmanaged class

T

ttc

Hi All,

I have a managed class that inherits from an unmanged class. The question
is, if the object of the manged class get garbage collected, will the memory
be free automatically for me or only the bit that is used by the managed
code? What about the unmanaged bit?

Does anyone know of any article that discuss about this?

Any help is appreciated.

Cuong Tong
 
N

Nicholas Paldino [.NET/C# MVP]

By unmanaged class, I assume that you mean a class that is imported
through COM interop.

If you derive from this class, and an instance of this class is GCed,
then the runtime will take care of releasing the reference that the runtime
callable wrapper holds on the interface instance for the COM object.
Assuming that is the only reference that exists, when the reference count is
zero, the COM object will dispose of itself.

However, just letting instances of your class be GCed like that isn't a
good idea, since you are leaving an unmanaged resource hanging around
waiting to be GCed. You would have to pass instances of your class to the
static ReleaseComObject method on the Marshal class when clients are done
using it, to make sure that the unmanaged reference is released ASAP when
you are done with it.
 
C

Cuong Tong

Hello Nicholas


Thanks for your help.

I should have explained myself a little bit better. Let me redraw the picture
again. By unmanaged resource I meant native c++ classes.

Basically I have some existing native c++, now I am writing some c++/cli
that inherits from those unmanaged classes.

I am just not so sure what would the garbage collector do with these kind
of classes.

Regards,

Cuong Tong
 
N

Nicholas Paldino [.NET/C# MVP]

For these kinds of classes, you shouldn't really extend from them, but
you should create managed wrappers for them which implement the IDisposable
interface. In your implementation of IDisposable, you would free the
instance of the class that you would create in the constructor (and hold a
pointer to in your instance).
 
C

Cuong Tong

Hello Nicholas ,


What you said does make sense. Could you please explain a little bit more
why I shouldn't really extend on these native classes? Writing a wrapper
for these native classes seems to be the way forward.

Regards,

Cuong Tong
 
W

Willy Denoyette [MVP]

Cuong Tong said:
Hello Nicholas ,


What you said does make sense. Could you please explain a little bit more
why I shouldn't really extend on these native classes? Writing a wrapper
for these native classes seems to be the way forward.

Managed classes cannot inherit from unmanaged classes, they both use
different object models with a different layout.
Note that this is not the right NG to ask questions about C++/C++CLI, you
might get better answers when posting to
microsoft.public.dotnet.languages.vc.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top