473,406 Members | 2,439 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,406 software developers and data experts.

ReaderWriterLockSlim must be Disposed()? What a Pain!

Friend,

I have a class that contains some data. The data will be accessed from
multiple threads of which 90% are reading and 10% are writing. A private
ReaderWriterLockSlim object is used within the class to facilitate
synchronization.

You're probably aware that ReaderWriterLockSlim implements IDisposable. So
to honor the contract with ReaderWriteLockSlim, it stands to reason that
*my* class must implement IDisposable so that when my class is disposed, so
is the private lock object.

Here's the problem. Various instances of my class are passed around from
place to place throughout the application and it's impossible for me as the
programmer to determine at precisely what point any given instance of the
class will no longer be referenced. I normally wouldn't care (that's what
the Garbage Collector is for) but in this case, I need to know so that
Dispose() can be called on my class.

The best solution I could come up with is to have my class maintain an
internal reference count. Each time the class is passed to some using
entity, AddRef() is called to increment the count and when that using entity
is done referencing my class, Release() is called to decrement the count.
When the count reaches zero, my class finally calls Dispose() on its
internal lock.

This seems ugly to me. There must be a better way. I know I could just skip
calling Dispose() on the lock and wait for it to be finalized, but who knows
when that'll happen? Maybe never.

Is there some kind of design pattern that's used in such cases?

Thanks.
Aug 25 '08 #1
1 2880
On Mon, 25 Aug 2008 16:26:25 -0700, Jules Winfield
<Ju************@newsgroup.nospamwrote:
[...]
You're probably aware that ReaderWriterLockSlim implements IDisposable.
So
to honor the contract with ReaderWriteLockSlim, it stands to reason that
*my* class must implement IDisposable so that when my class is disposed,
so
is the private lock object.

[...]
The best solution I could come up with is to have my class maintain an
internal reference count. Each time the class is passed to some using
entity, AddRef() is called to increment the count and when that using
entity
is done referencing my class, Release() is called to decrement the count.
When the count reaches zero, my class finally calls Dispose() on its
internal lock.

This seems ugly to me. There must be a better way. I know I could just
skip
calling Dispose() on the lock and wait for it to be finalized, but who
knows
when that'll happen? Maybe never.

Is there some kind of design pattern that's used in such cases?
Ref-counting is a reasonably tried-and-true design. Granted, I prefer not
to use it in C#/.NET programming, because it's a bit contrary to the whole
garbage-collecting paradigm. But there's not really anything
fundamentally wrong with it (other than it being error-prone :) ).

IMHO, the preferable solution is to create the design so that there's
clear ownership of an object, in a single place of code. That is, if
you've got code that's implementing multiple clients of this shared,
disposable object, then that code shouldn't care at all about disposing
it. There should be some single-point code that manages the object, and
which itself is already aware of the status of the clients.

For example, if you've got multiple readers, there presumably there's some
other code that knows how many readers there are, what their lifetime is,
etc. That other code is where ownership of the shared, disposable object
should be IMHO.

But if for some reason that doesn't work for you, ref-counting seems
reasonable to me. I think I'd prefer that solution to that of using the
disposal of the object as a signal for the clients to stop using it (by
catching the ObjectDisposedException). :)

Pete
Aug 25 '08 #2

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

Similar topics

7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
9
by: David Sworder | last post by:
Hi, I have a form that displays data (is that vague enough for you?). The data comes in on a thread-pool thread. Since the thread pool thread is not the same as the UI thread, the callback...
5
by: theinvisibleGhost | last post by:
I'm having a problem that occurs at random in my app. I get an exception "Cannot Access a disposed object" In MSCorLib when calling boolean Change (int32, int32) Stack trace reveals...
6
by: SamSpade | last post by:
Public Function PicCreateGraphics() As Graphics 'Client should dispose this PicCreateGraphics = Graphics.FromImage(mDocumentImage) mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can...
1
by: osmarjunior | last post by:
How to ensure that a form is disposed? When I work with modal forms I do the following: using (Form1 frm = new Form1()) { frm.ShowDialog(); } As far as I read, when it ends the using scope,...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
31
by: Zytan | last post by:
There is no IsDisposed() method. I could just access it, and catch ObjectDisposedException, but that seems ugly. Zytan
1
by: Jules Winfield | last post by:
Friend, I have a class that contains some data. The data will be accessed from multiple threads of which 90% are reading and 10% are writing. A private ReaderWriterLockSlim object is used...
2
by: BillE | last post by:
Using vb.net 2008 windows forms. How can a form which has been closed and disposed still attempt to handle a custom event? I have a form which inherits from a base form class. The base...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...

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.