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

  • SEARCH
  • Home
  • 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 3240254
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:03:51+00:00 2026-05-17T18:03:51+00:00

I have the following code: A disposable class that contains two disposable members. One

  • 0

I have the following code:
A disposable class that contains two disposable members.
One of them is being initialized using new() method, and the other using static factory method.
I Also have static code analisys rules, with CA2213 as error.

    public class DisposableClass : IDisposable
{
    private WebClient m_DisposableMember1;

    public WebClient DisposableMember1
    {
        get
        {
            if (m_DisposableMember1 == null)
            {
                m_DisposableMember1 = new WebClient();
            }
            return m_DisposableMember1;
        }
    }

    private WebClient m_DisposableMember2;

    public WebClient DisposableMember2
    {
        get
        {
            if (m_DisposableMember2 == null)
            {
                m_DisposableMember2 = Factory.Create();
            }
            return m_DisposableMember2;
        }
    }


    #region Finalize/Dispose Pattern
    private bool m_IsDisposed = false;

    //Implement IDisposable.
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    ~DisposableClass()
    {
        Dispose(false);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!m_IsDisposed)
        {
            if (disposing)
            {
                DisposableMember1.Dispose();
                // DisposableMember2 in not disposed and not notified by fxCop
            }

            m_IsDisposed = true;
        }
    }
    #endregion Finalize/Dispose Pattern

}

This is the simple factory class:

    public static class Factory
{
    public static WebClient Create()
    {
        return new WebClient();
    }
}

When I call the Dispose() method of the DisposableMember1 property, I get CA2213.
When I call the Dispose() method of the m_DisposableMember1 member, I don’t get this error.

More over, I don’t get this error for m_DisposableMember2 (Wich was initialized using the static factory), and it is not being disposed.

Does anybody familiar with this issue? What can cause this behavior?

  • 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-05-17T18:03:52+00:00Added an answer on May 17, 2026 at 6:03 pm

    CA2213 is not particularly aggressive about identifying fields that ought to be disposed. Amongst other potential problems, it only considers fields that are directly assigned from a constructor invocation. Assignment of a field value via a property or from a source other than a constructor invocation do not result in the target field being included in the pool of “should be disposed” fields by the rule.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
Let say I have the following desire, to simplify the IConvertible's to allow me
I have a project that adds elements to an AutoCad drawing. I noticed that
I have found this example on StackOverflow: var people = new List<Person> { new
My question is about memory use and objects in actionscript 2. If I have
I am attempting to pull some information from my tnsnames file using regex. I
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a snippet to create a 'Like' button for our news site: <iframe
After having read Ian Boyd 's constructor series questions ( 1 , 2 ,

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.