Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:36:06+00:00 2026-06-18T07:36:06+00:00

I have a class MyClass . This class have a field: public ReaderWriterLockSlim rw;

  • 0

I have a class MyClass. This class have a field: public ReaderWriterLockSlim rw; (public for an easier example code). Many threads can read data from MyClass using rw.EnterReadLock etc.

Also I have implemented IDisposable interface:

private void Dispose (bool pDisposing)
{
    {
        if (pDisposing) // release managed resources
        {
            if (rw != null)
            {
                rwLockSlim.Dispose ();
                rwLockSlim = null;
            }
        }

        //--- release unmanaged resources
        // some code...
        isDisposed = true; // ...
    }
}

Like you see, problem is when one thread is using MyClass when second thread call Dispose on myClass object. I cannot dispose ReaderWriterLockSlim because it will crash my application. So should I just remove that lines which releases managed resources? Anyway that ReaderWriterLockSlim will be collected by GC in near future, right? (but is this class resource expensive?).
Maybe I should add some lock(syncObject) in Dispose metod or something?

EDIT: also I deal with AllocHGlobal, so I need to wait, until all threads will stop reading/writing to myClass.

Different point of view:

public MyClass : IDisposable
{
            public void EnterReadLock (); // calls rwLockSlim.EnterReadLock,  
                                          // if object is disposed throws Exception

            public void ExitReadLock ();  // same as above

            public void Dispose ();       // wait until all threads exit from locks,
                                          // frees unamanged resources, mark class as disposed
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:36:08+00:00Added an answer on June 18, 2026 at 7:36 am

    This may not be the best answer, rather observations and some thoughts.

    Can you simplify your code? I mean, other types shouldn’t care if your type throws exceptions under specific concurrency conditions. How will you test this? Public lock object is evil. It must be private unless you want to spent months trying to figure out mysterious bugs.

    If a dispose method is called, it means that no other objects should ever use this object. Which means you must ensure first that all threads finished operating on the object first, before calling a dispose method.

    Suggestions

    • Make the lock private
    • Ensure all threads are finished, before calling to dispose
    • Add timeouts for safe behaviour
    • Consider using static lock object
    • ReaderWriterLockSlim is not resource expensive (to the point you should care about in your application)
    • If your class uses disposable resources, it is usually better to implement IDisposable and dispose members that need to be disposed.
    • Don’t add any locks in the Dispose method, it shall be simple. This may introduce unpleasant bugs. Because if you forget to call a Dispose manually, it will be non-deterministically called by GC (through a finiliser, which you should add as well to call Dispose)
    • Correct approach would be to wait until all threads are done and dispose an object
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =
For example: I have a class MyClass //Dirty code { public int Id; public
I have this code: public class MyClass { public int X { get; set;
Say I have this class: class myclass { public int Field1{ get; set; }
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
Currently I have a class that looks like this: public class MyClass : IMyClass
So I have a model like this: class MyClass { public $customer = null;
I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)
I have the following piece of code: public class MyClass { class Inner {
I have a class like this: private class MyClass { [DisplayName(Foo/Bar)] public string FooBar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.