Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

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

Hi,

using a VisaNS Session object, one can invoke Dispose() to make sure that the underlying unmanaged VISA session is immediately closed instead of waiting for the GC to invoke the Session finalizer code.

Now suppose I use a reference to a Session object 's1' in another object 'dsu1' of class DirectSessionUser and that I have two additional objects 'isu1' and 'isu2' of class IndirectSessionUser which reference 'dsu1' and as such indirectly reference 's1'. All of these objects directly or indirectly use the underlying unmanaged VISA session.

Now I want to close this VISA session as soon as none of the above objects 's1', 'dsu1', 'isu1' and 'isu2' need this session.
If there would only be 's1' and 'dsu1' I would implement the IDisposeable interface for DirectSessionUser, which in its turn would invoke s1.Dispose(). So dsu1.Dispose() will do the job.

As soon as other objects are involved like 'isu1' and 'isu2', how can I safely invoke Dispose() on 's1', when I don't want to wait for the GC to kick in ?
Safely means that 'isu1' and 'isu2' must indicate that they no longer need 'dsu1' and as such 's1'.  Anyone any ideas or am I talking complete nonsense here ... Smiley Sad

Thanks.

0 Kudos
Message 1 of 4
(3,651 Views)
Hi Frans

Are the multiple isu classes sharing a reference to a single VISA session or are they creating their own session objects? If that is the case, you can dispose the object in one class, but then you must alway check the state of the session object using IsDisposed before using it anywhere.



Bilal Durrani
NI
0 Kudos
Message 2 of 4
(3,632 Views)

Hi Bilal,

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

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 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 (C) VISA session 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 (C) VISA session was closed, ready for immediate use by someone else.

Unfortunately Microsoft decided not to use ref counting (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 humble knowledge Smiley Wink ) 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 Session is no longer referenced (either directly or indirectly).  Verifying IsDisposed() - each time I want to use the Session - is not really a solution ... Smiley Sad

Perhaps there are clever guys out there who can convince me that indeed there is a clean solution to my problem in .NET ...

Thanks again,

Frans.

0 Kudos
Message 3 of 4
(3,621 Views)
For your use case, it looks like the best thing to do would be to write up your own ref counting resource allocation system. The Dispose pattern does have its limitations and this kind of resource management is one of them.

Bilal Durrani
NI
0 Kudos
Message 4 of 4
(3,596 Views)