Re: Managed class inherit from unmanaged class



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



.



Relevant Pages

  • Re: Managed class inherit from unmanaged class
    ... 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. ... I have a managed class that inherits from an unmanged class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Managed class inherit from unmanaged class
    ... 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. ... then the runtime will take care of releasing the reference that ... Assuming that is the only reference that exists, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: memory leak in script??
    ... for a good reason -- i.e., ... "reference count" (that is, the number of variables ... count set up by the first sub. ... but for the purpose of releasing the processor ...
    (microsoft.public.scripting.vbscript)
  • Re: COM unloading problem with circular reference.
    ... object before releasing it. ... you need to partition your reference counting so you can know ... references there are to the interface side and a separate count for the sink ... sometimes called a listener or event helper object. ...
    (microsoft.public.win32.programmer.ole)
  • Re: COM unloading problem with circular reference.
    ... and the other is internal - your sink for C. ... object before releasing it. ... you need to partition your reference counting so you can know ... sometimes called a listener or event helper object. ...
    (microsoft.public.win32.programmer.ole)