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 1048949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:32:16+00:00 2026-05-16T16:32:16+00:00

I have an object that uses some underlying native resources, and has a pointer

  • 0

I have an object that uses some underlying native resources, and has a pointer to the next instance, which I iterate through similar to:

MyObject begin = null;

try
{
    begin = GetFirst();

    while (begin != null)
    {
        MyObject next = begin.Next();
        // do something with begin
        begin.Dispose();
        begin = next;
    }
}
finally
{    
    if (begin != null)
    {
        begin.Dispose();
    }
}

I get the code analysis problem:

CA2202: Microsoft.Usage: Object ‘begin’ can be disposed more than once in method ‘x()’. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.

Any idea how I can get rid of this error without suppressing it?

  • 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-16T16:32:17+00:00Added an answer on May 16, 2026 at 4:32 pm

    It certainly seems to me that your last block of code is unnecessary. If begin != null, then your while loop should have continued, no?

    UPDATE: It looks like you’re trying to ensure the last obtained value for begin is disposed in case an exception is thrown. Try this:

    MyObject begin = GetFirst();
    
    while (begin != null)
    {
        MyObject next;
        using (begin)
        {
            next = begin.Next();
            // do something with begin
        }
    
        begin = next;
    }
    

    Note that in the above suggestion, it could actually still happen that you end up with an undisposed object: the last value assigned to next, before the end of the using block. This scenario wasn’t covered in your original question, so I haven’t addressed it in the above suggestion. It’s something to consider, though, if that’s a potential problem.

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

Sidebar

Related Questions

I have an object that is expensive to create, which uses some unmanaged resources
I have an <iframe> that uses some variables that are set in an object
I have an ASP.net application that uses some common business object that we have
For some reason I have having an issue compiling a shared object that uses
I have a very simple Player class in java that uses some strategies object
I have some code that uses the SQL Server 2005 SMO objects to backup
I have a web application that uses the CDO Message object to email reports.
I have a flex 3 app that uses netstream and a video object to
I currently have a Javascript function that uses a string to reference an object
I have created an app in C# .Net 2.0 that uses the AxShockwaveFlash object

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.