473,386 Members | 1,715 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,386 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 1136
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.