473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Managed class inherit from unmanaged class

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

Dec 9 '07 #1
5 3856
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ttc" <cu********@gmail.comwrote in message
news:FA**********************************@microsof t.com...
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
Dec 9 '07 #2
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" <cu********@gmail.comwrote in message
news:FA**********************************@microsof t.com...
>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

Dec 9 '07 #3
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]
- mv*@spam.guard.caspershouse.com

"Cuong Tong" <cu********@gmail.comwrote in message
news:48*************************@msnews.microsoft. com...
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" <cu********@gmail.comwrote in message
news:FA**********************************@microso ft.com...
>>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

Dec 9 '07 #4
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
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).

"Cuong Tong" <cu********@gmail.comwrote in message
news:48*************************@msnews.microsoft. com...
>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" <cu********@gmail.comwrote in message
news:FA**********************************@micros oft.com...
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

Dec 9 '07 #5
"Cuong Tong" <cu********@gmail.comwrote in message
news:48**************************@msnews.microsoft .com...
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.

Dec 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
2
by: Neil | last post by:
I am developing a demo in C# using Managed DirectX. I wanted to use a C++ static library in the demo (its a class for handling physics), so I decided to create my Graphics classes in C#, inherit...
5
by: Chris Kiechel | last post by:
I am writing a .NET Windows application and it needs to perform DDE calls to a legacy system. I created a C++ unmanaged class that performs the actual DDE connection and communication. However,...
2
by: mats | last post by:
Hi! This is a quite involved question concerning two dll's that are consumed from a console application. The first dll is called base.dll and is compiled in mixed mode (managed and unmanaged...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
3
by: nikola | last post by:
is it possible to export (and, above all, import afterwards) an unmanaged class from a managed dll (compiled with the /clr switch. it would in fact probably result in a mixed mode dll)? it seems that...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
13
by: bonk | last post by:
Hello, I am trying to create a dll that internally uses managed types but exposes a plain unmanaged interface. All the managed stuff shall be "wrapped out of sight". So that I would be able to...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.