473,322 Members | 1,496 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,322 software developers and data experts.

Dispose question

Hi,

I wrote a manage c++ wrapper classes that wraps an unmanaged c++ classes.
if my unmanaged c++ classes don't used any critical resources , do i still
need to make my managed c++ wrapper classes to implement IDisposable just
because they are wrapping an unmanage c++ class ?

Thanks.
Nov 17 '05 #1
5 1131
Hi yaron!
I wrote a manage c++ wrapper classes that wraps an unmanaged c++ classes.
if my unmanaged c++ classes don't used any critical resources , do i still
need to make my managed c++ wrapper classes to implement IDisposable just
because they are wrapping an unmanage c++ class ?


If you do not need to free memory or any other stuff, you do not need to
implement IDisposable.

In general you can say: If every method/property in your wrapper class
always instanciate a new unmanaged-class then also frees it, you do not
need an IDisposable...

If you instanciate your unmanaged class only once (for example in the
managed constructor) you should implement IDisposable to free this
instance. I do not suggest to use the finilazier, because then you will
be called from a different thread (if at all) and some unmanaged classes
have some troubles with this...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2
Hi Jochen Kalmbach,

but is this is not the work of the GC to free the memory of my __gc wrapper
classes and the unmanage classes they are holding pointers to ?

Thanks.
"Jochen Kalmbach [MVP]" wrote:
Hi yaron!
I wrote a manage c++ wrapper classes that wraps an unmanaged c++ classes.
if my unmanaged c++ classes don't used any critical resources , do i still
need to make my managed c++ wrapper classes to implement IDisposable just
because they are wrapping an unmanage c++ class ?


If you do not need to free memory or any other stuff, you do not need to
implement IDisposable.

In general you can say: If every method/property in your wrapper class
always instanciate a new unmanaged-class then also frees it, you do not
need an IDisposable...

If you instanciate your unmanaged class only once (for example in the
managed constructor) you should implement IDisposable to free this
instance. I do not suggest to use the finilazier, because then you will
be called from a different thread (if at all) and some unmanaged classes
have some troubles with this...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #3
Hi yaron!
but is this is not the work of the GC to free the memory of my __gc wrapper
classes and the unmanage classes they are holding pointers to ?


Yes. The GC will call your finilizer if GC think it should. But this
will be done from a different thread... and it also might never called
(for example at some circumstances at shutdown)...

It is better, if you provide IDisposable _and_ implement the
finalizer... so the user can either call Dispose or if the user forgets,
the finalizer will be called.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #4
Hi Jochen Kalmbach,

but is this is not the work of the GC to free the memory of my __gc wrapper
classes and the unmanage classes they are holding pointers to ?

Thanks.
"Jochen Kalmbach [MVP]" wrote:
Hi yaron!
I wrote a manage c++ wrapper classes that wraps an unmanaged c++ classes.
if my unmanaged c++ classes don't used any critical resources , do i still
need to make my managed c++ wrapper classes to implement IDisposable just
because they are wrapping an unmanage c++ class ?


If you do not need to free memory or any other stuff, you do not need to
implement IDisposable.

In general you can say: If every method/property in your wrapper class
always instanciate a new unmanaged-class then also frees it, you do not
need an IDisposable...

If you instanciate your unmanaged class only once (for example in the
managed constructor) you should implement IDisposable to free this
instance. I do not suggest to use the finilazier, because then you will
be called from a different thread (if at all) and some unmanaged classes
have some troubles with this...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #5
Hi yaron!
but is this is not the work of the GC to free the memory of my __gc wrapper
classes and the unmanage classes they are holding pointers to ?


Yes. The GC will call your finilizer if GC think it should. But this
will be done from a different thread... and it also might never called
(for example at some circumstances at shutdown)...

It is better, if you provide IDisposable _and_ implement the
finalizer... so the user can either call Dispose or if the user forgets,
the finalizer will be called.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #6

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

Similar topics

11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
4
by: xyu | last post by:
Hello, First I would like to thank anyone who helps me, much appreciated. I'm a c++ programmer just started using c#. I'm writing some big hash table so I want to actively take my object off...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
13
by: Grafix | last post by:
All - As we all know, Dispose method is reserved in C++ 8 and the expected syntax is to use ~MyClass(). In 1.1, we used to have following structure for Dispose class MyClass : IDisposable {...
1
by: jinfeng_Wang | last post by:
hi, I have a question about the difference between SqlConnection.IDisposable.Dispose() and SqlConnection.Dispose(). Both of them realize the function of releasing the connection to the...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
3
by: AlexS | last post by:
When I implement Dispose pattern in object implementing IDisposable, current fxcop recommends: Ensure that Wrapper.Dispose():Void is declared as public and sealed. However, if I do as it asks,...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
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
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.