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