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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:39:20+00:00 2026-06-12T15:39:20+00:00

We have code like below to fix CA2000: Dispose objects before losing scope (

  • 0

We have code like below to fix CA2000: Dispose objects before losing scope (http://msdn.microsoft.com/en-us/library/ms182289.aspx). When the tbl is assigned null, will the object that it referenced garbage collected ?

    private DataTable BuildRequestDataTable(Factory tableFactory)
    {
        DataTable tbl = null;
        DataTable requestTable = null;
        try
        {
            tbl = tableFactory.CreateTable();
            requestTable = tbl;
            tbl = null;
        }
        finally
        {
            if (tbl != null)
            {
                tbl.Dispose();
            }
        }

        return requestTable;
    }
  • 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-12T15:39:21+00:00Added an answer on June 12, 2026 at 3:39 pm

    When the tbl is assigned null, will the object that it referenced garbage collected ?

    No. Or at least, not immediately. Objects aren’t reference-counted like that.

    It’s not really clear why you’re juggling all those variables at all – I can’t see how you’d ever end up disposing of anything. Either the CreateTable call succeeds, in which case the following two statements will succeed and nothing will be disposed, or the CreateTable call throws, in which case tbl will still be null and nothing will be disposed.

    Your code is basically equivalent to:

    private DataTable BuildRequestDataTable(Factory tableFactory)
    {
        return tableFactory.CreateTable();
    }
    

    At that point you may well get a warning that you’re not disposing of the DataTable, but that’s deliberate – the caller would presumably take responsibility for disposing it.

    Of course at this point it’s not clear that the code is really providing much benefit itself – it’s no easier to use:

    using (var table = BuildRequestDataTable(tableFactory))
    

    than

    using (var table = tableFactory.CreateTable())
    

    … unless you’re planning to add more logic in the method.

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

Sidebar

Related Questions

I have a standard code like below to validate xml against xsd, but it
I have a code like this below, which gives me a $link that equals
I have a code like this below in /root_project/main.cpp : #include theoraplayer/TheoraVideoClip.h unsigned int
I have code that looks more or less like the code below but it
I have something like the code below: for(int i=0;i<10;i++){ button=new JButton(buttons[i]); button.addActionListener(new ActionListener(){ public
Hi I have below pseudo code with throws an exception like this throw new
I have made a YUI module a little like the code shown below, which
I have a data that looks like this . And my code below simply
i have a code in c# like below string s = new string('~',25); int
I have a code like shown below if ((client == NULL) || (client->type ==

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.