Re: Managed class inherit from unmanaged class
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 9 Dec 2007 17:54:57 -0500
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Cuong Tong" <cuong.tong@xxxxxxxxx> wrote in message news:487d4b97fe3a8ca0927df1d6c29@xxxxxxxxxxxxxxxxxxxxxxx
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
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.
"ttc" <cuong.tong@xxxxxxxxx> wrote in message
news:FA7863CA-C2B6-4021-ABC1-6C81424D4A01@xxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: Managed class inherit from unmanaged class
- From: Cuong Tong
- Re: Managed class inherit from unmanaged class
- References:
- Re: Managed class inherit from unmanaged class
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Managed class inherit from unmanaged class
- From: Cuong Tong
- Re: Managed class inherit from unmanaged class
- Prev by Date: Re: OOP Question
- Next by Date: Re: At a loss with Media Player
- Previous by thread: Re: Managed class inherit from unmanaged class
- Next by thread: Re: Managed class inherit from unmanaged class
- Index(es):
Relevant Pages
|