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

Using Dispose() ... don't wait until GC kicks in.

Hi,

I'm working on an application where it is essential to free underlying
non-memory resources as soon as they are no longer needed. In my case, it is
a VISA resource which is used in automated tests (involving instruments), but
my question is relevant for any other resource, like those held by a
FileStream. The problem that I'm facing is that this resource is used, either
directly or indirectly, by different objects.

Let me rephrase my problem from a C++ point of view.

In the past (long before something similar showed up in COM) I implemented
smart pointers/handlers (implemented as objects) with automatic ref count
increments and decrements by overloading the copy constructor, assignment
operator, etc. From that moment on I didn't have to worry about freeing
memory too soon or too late. In the case of a resource encapsulated in a C++
object (smart pointer/handle), I didn't have to worry how many times it was
referenced. As soon as it was no longer referenced, the destructor was
invoked and the resource was freed, ready for immediate use by someone else.

Unfortunately (for me) Microsoft decided not to use ref counting in .NET
(e.g. to avoid issues in the case of circular references, which fortunately I
didn't have to deal with) ... so I tried to find a clean method which would
do something similar in C# as I was able to do using C++ (after adding smart
pointers/handles). The only way - to my knowledge - is to use the Dispose()
method in order not to wait for the GC to kick in at an undetermined moment
in the near or far future ... However, at that moment - again to my humble
opinion - there is no way to know that the resource is no longer referenced
(either directly or indirectly). Some C# classes implement bool IsDisposed()
to verify if an object is already disposed or one can catch the
ObjectDisposedException exception. To me however this is not really a clean
solution.

I'm looking forward to learn from clever guys out there to demonstrate that
indeed there is a clean solution to my problem in .NET ...

Thanks for taking the time to read my post and many thanks in advance.
Frans.
Feb 16 '07 #1
7 1776
Frans wrote:

[IDisposable resource]
The problem that I'm facing is that this resource is used, either
directly or indirectly, by different objects.
I'm looking forward to learn from clever guys out there to demonstrate that
indeed there is a clean solution to my problem in .NET ...
The answer is to either reorganize so there's a single well-defined
owner of the disposable resource, or implement a protocol for disposing
of the resource.

For example, consider implementing an IDisposable handle which disposes
the resource when all outstanding handles have been disposed. That way,
the problem of n-owners of a single resource is reduced to n instances
of 1-ownership, and 1-ownership is well understood through either
'using' or chained disposal via implementing IDisposable and a protected
virtual void Disposing(bool).
Some C# classes implement bool IsDisposed()
to verify if an object is already disposed or one can catch the
ObjectDisposedException exception.
BTW, I think your comments on ObjectDisposedException are a red herring.
It is not an error to dispose an already disposed object, but if you use
a disposed object, you've got a logic error (i.e. you disposed too
early), not a lack of an 'IsDisposed' property.

-- Barry

--
http://barrkel.blogspot.com/
Feb 16 '07 #2
Hey Barry,

first of all thanks for taking a look at my post.

Please correct me if I'm incorrectly interpreting your answer, but aren't
you proposing to implement some kind of reference counting ?

Also another problem that I see is that someone outside the class
(encapsulating the resource) must invoke Dispose(), while in my C++
equivalent, this is automatically dealt with by the destructor of the class
(encapsulating the resource) itself.

With respect to the red herring ;-) ... apparently I was not fully clear ...
what I meant is fully consistent with your feedback (although one typically
prevents that Dispose is invoked more than once): don't use a disposed object.

Thanks.
Frans.
Feb 16 '07 #3
Don't forget to use the "using" statement when implementing your class, so
it will be automatically disposed when it falls out of scope.
"Frans" <Fr***@discussions.microsoft.comwrote in message
news:34**********************************@microsof t.com...
Hey Barry,

first of all thanks for taking a look at my post.

Please correct me if I'm incorrectly interpreting your answer, but aren't
you proposing to implement some kind of reference counting ?

Also another problem that I see is that someone outside the class
(encapsulating the resource) must invoke Dispose(), while in my C++
equivalent, this is automatically dealt with by the destructor of the
class
(encapsulating the resource) itself.

With respect to the red herring ;-) ... apparently I was not fully clear
...
what I meant is fully consistent with your feedback (although one
typically
prevents that Dispose is invoked more than once): don't use a disposed
object.

Thanks.
Frans.

Feb 16 '07 #4
"Scott M." wrote:
Don't forget to use the "using" statement when implementing your class, so
it will be automatically disposed when it falls out of scope.
Hey Scott,

I considered this too, but "using" is only a viable solution when the
resource is uniquely used within a small and well-defined portion (the scope
defined by the "using" statement) of the code, no ?

In my case, this is not possible.

Thanks anyway.
Frans.
Feb 16 '07 #5
Yes, your are correct Frans.
"Frans" <Fr***@discussions.microsoft.comwrote in message
news:B6**********************************@microsof t.com...
"Scott M." wrote:
>Don't forget to use the "using" statement when implementing your class,
so
it will be automatically disposed when it falls out of scope.

Hey Scott,

I considered this too, but "using" is only a viable solution when the
resource is uniquely used within a small and well-defined portion (the
scope
defined by the "using" statement) of the code, no ?

In my case, this is not possible.

Thanks anyway.
Frans.

Feb 16 '07 #6
Frans wrote:
Hey Barry,

first of all thanks for taking a look at my post.

Please correct me if I'm incorrectly interpreting your answer, but aren't
you proposing to implement some kind of reference counting ?
That's right. It's rather hard to deterministically dispose of a
resource with multiple owners without counting the number of owners, and
disposing of the resource when the owner count reaches zero. I think an
argument could be made that any deterministic resource disposal
mechanism for resources with multiple owners can be described as
"reference counting".
Also another problem that I see is that someone outside the class
(encapsulating the resource) must invoke Dispose(), while in my C++
equivalent, this is automatically dealt with by the destructor of the class
(encapsulating the resource) itself.
Sure. C# isn't C++. If this is a problem for you, you can use C++/CLI.

:)

-- Barry

--
http://barrkel.blogspot.com/
Feb 17 '07 #7
Frans,

Are you sure that you can use Net or even more facilities from the windows
operatingsystem. I get the idea that the best thing you can do is strip your
OS down to those services strictly needed.

The time needed to dispose is probably much to long and will interupt your
processes to free your resources and will therefore probably be the horse
behind the cart (and even in the wrong direction).

This was something I was thinking about reading your problem.

Cor

>
I'm working on an application where it is essential to free underlying
non-memory resources as soon as they are no longer needed. In my case, it
is
a VISA resource which is used in automated tests (involving instruments),
but
my question is relevant for any other resource, like those held by a
FileStream. The problem that I'm facing is that this resource is used,
either
directly or indirectly, by different objects.

Let me rephrase my problem from a C++ point of view.

In the past (long before something similar showed up in COM) I implemented
smart pointers/handlers (implemented as objects) with automatic ref count
increments and decrements by overloading the copy constructor, assignment
operator, etc. From that moment on I didn't have to worry about freeing
memory too soon or too late. In the case of a resource encapsulated in a
C++
object (smart pointer/handle), I didn't have to worry how many times it
was
referenced. As soon as it was no longer referenced, the destructor was
invoked and the resource was freed, ready for immediate use by someone
else.

Unfortunately (for me) Microsoft decided not to use ref counting in .NET
(e.g. to avoid issues in the case of circular references, which
fortunately I
didn't have to deal with) ... so I tried to find a clean method which
would
do something similar in C# as I was able to do using C++ (after adding
smart
pointers/handles). The only way - to my knowledge - is to use the
Dispose()
method in order not to wait for the GC to kick in at an undetermined
moment
in the near or far future ... However, at that moment - again to my humble
opinion - there is no way to know that the resource is no longer
referenced
(either directly or indirectly). Some C# classes implement bool
IsDisposed()
to verify if an object is already disposed or one can catch the
ObjectDisposedException exception. To me however this is not really a
clean
solution.

I'm looking forward to learn from clever guys out there to demonstrate
that
indeed there is a clean solution to my problem in .NET ...

Thanks for taking the time to read my post and many thanks in advance.
Frans.

Feb 18 '07 #8

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

Similar topics

26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
3
by: Giovanni Bassi | last post by:
Hello Group, I am running an operation in a different thread. There are resources that are released when the thread is done running. This is done at the end of the execution as it raises an...
6
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged...
2
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the...
3
by: Dave | last post by:
I trying to determine the best pattern for designing my business and data layers... Can the instance of the business object eventually cause memory leaks in Example 1? If your business class...
5
by: Michael Sander | last post by:
Hi, I found out, that a simple Form, created only with the designer, wont Dispose its contextmenu if you show the form, right-click it, and dispose the form afterwards. This isnt really what I...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
17
by: R.Rafii | last post by:
Hi, I have a simple (?) question for you all experts. I have a button that performs a query on my SQL and fill a datagrid on the form The code: Dim sconn As New SqlConnection()...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.