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. 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




.



Relevant Pages

  • 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: Managed class inherit from unmanaged class
    ... By unmanaged resource I meant native c++ classes. ... then the runtime will take care of releasing the reference that ... Assuming that is the only reference that exists, ... I have a managed class that inherits from an unmanged class. ...
    (microsoft.public.dotnet.languages.csharp)
  • 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)
  • Re: object clean up ?
    ... You should call IDisposable.Disposeto inform the object you're done with it and it should do any cleanup it needs to immediaetly. ... Setting the reference to null doesn't inform the object in any way. ... I inherit the class from IDisposable interface and implement ... Dispose method. ...
    (microsoft.public.dotnet.framework)

Quantcast