Click here to Skip to main content
15,886,026 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to acheive this in LINQ? [modified] Pin
Subin Mavunkal25-May-11 3:12
Subin Mavunkal25-May-11 3:12 
QuestionIDisposable and CA2000 warning during Vs2010 Code Analysis. Pin
gumi_r@msn.com24-May-11 1:32
gumi_r@msn.com24-May-11 1:32 
AnswerRe: IDisposable and CA2000 warning during Vs2010 Code Analysis. Pin
Subin Mavunkal24-May-11 2:23
Subin Mavunkal24-May-11 2:23 
GeneralRe: IDisposable and CA2000 warning during Vs2010 Code Analysis. Pin
Pete O'Hanlon24-May-11 2:27
mvePete O'Hanlon24-May-11 2:27 
GeneralRe: IDisposable and CA2000 warning during Vs2010 Code Analysis. Pin
gumi_r@msn.com24-May-11 2:39
gumi_r@msn.com24-May-11 2:39 
GeneralRe: IDisposable and CA2000 warning during Vs2010 Code Analysis. Pin
Subin Mavunkal24-May-11 2:46
Subin Mavunkal24-May-11 2:46 
GeneralRe: IDisposable and CA2000 warning during Vs2010 Code Analysis. Pin
Pete O'Hanlon24-May-11 2:48
mvePete O'Hanlon24-May-11 2:48 
GeneralRe: IDisposable and CA2000 warning during Vs2010 Code Analysis. [modified] Pin
gumi_r@msn.com24-May-11 3:28
gumi_r@msn.com24-May-11 3:28 
I'm sorry but that is wrong.

A use statement is just syntactic sugar for a try finally block. What you are proposing translates to:

Bar bar = null;
try
{
  bar = new Bar();
  ...
  return new FooX(bar);
}
finally
{
  if (bar != null)
  {
    bar.Dispose();
  }
}


When the method gets to the return statement, it will execute the finally clause before exiting the method's scope returning a FooX object with a disposed bar.

If you are not convinced execute the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tests
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Creating FooA 'fooA' from GetFooAWithUsingStatement.");
            FooA fooA=FooProvider.GetFooAWithUsingStatement();
            Console.WriteLine("'fooA' internal bar is disposed: {0}", fooA.InternalBarIsDisposed);
            Console.WriteLine("Disposing 'fooA'");
            fooA.Dispose();
            Console.WriteLine("'fooA' internal bar is disposed: {0}", fooA.InternalBarIsDisposed);
            Console.WriteLine();
            Console.WriteLine("Creating FooA 'fooA' from GetFooAWithoutUsingStatement.");
            fooA = FooProvider.GetFooWithoutUsingStatement();
            Console.WriteLine("'fooA' internal bar is disposed: {0}", fooA.InternalBarIsDisposed);
            Console.WriteLine("Disposing 'fooA'");
            fooA.Dispose();
            Console.WriteLine("'fooA' internal bar is disposed: {0}", fooA.InternalBarIsDisposed);
            Console.Write("Press a key to exit.");
            Console.ReadKey();
        }
    }


    class Bar : IDisposable
    {
        bool disposed;

        public void Dispose()
        {
            this.disposed = true;
        }

        public bool IsDisposed { get { return this.disposed; } }
    }

    static class FooProvider
    {
        public static FooA GetFooAWithUsingStatement()
        {
            using (Bar bar = new Bar())
            {
                return new FooA(bar);
            }
        }

        public static FooA GetFooWithoutUsingStatement()
        {
            Bar bar = new Bar();
            return new FooA(bar);
        }
    }

    class FooBase : IDisposable
    {
        bool disposed;
        Bar bar;

        internal FooBase(Bar bar)
        {
            this.bar = bar;
        }

        public void Dispose()
        {
            Dispose(true);
            this.disposed = true;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.bar.Dispose();
            }
        }

        public bool IsDisposed { get { return this.disposed; } }
        public bool InternalBarIsDisposed { get { return this.bar.IsDisposed; } }
    }

    class FooA : FooBase
    {
        public FooA(Bar bar)
            : base(bar)
        {
        }
    }
}


To make things clear I should clarify that 'FooX' needs 'bar' to not be disposed during its lifetime. I thought I had made myself clear indirectly by needing to dispose 'bar' in 'FooBase.Dispose()' method.

My question remains the same: Is the Warning I'm getting valid or is the Analyzer not understanding the way the code really works and I can supress it even if MS guidelines says you should not do so?

Edit: typos (not all by any means)
modified on Tuesday, May 24, 2011 9:37 AM

GeneralRe: IDisposable and CA2000 warning during Vs2010 Code Analysis. Pin
Subin Mavunkal24-May-11 3:40
Subin Mavunkal24-May-11 3:40 
GeneralRe: IDisposable and CA2000 warning during Vs2010 Code Analysis. Pin
DaveyM6924-May-11 23:40
professionalDaveyM6924-May-11 23:40 
QuestionBar Chart In crystal Report Pin
Member 793274523-May-11 22:19
Member 793274523-May-11 22:19 
QuestionUsing Wild Character _ [modified] Pin
Member 455049323-May-11 5:34
Member 455049323-May-11 5:34 
AnswerRe: Using Wild Character _ Pin
Luc Pattyn23-May-11 5:55
sitebuilderLuc Pattyn23-May-11 5:55 
GeneralRe: Using Wild Character _ Pin
Member 455049324-May-11 0:30
Member 455049324-May-11 0:30 
GeneralRe: Using Wild Character _ [modified] Pin
Luc Pattyn24-May-11 0:56
sitebuilderLuc Pattyn24-May-11 0:56 
GeneralRe: Using Wild Character _ Pin
Pete O'Hanlon24-May-11 1:03
mvePete O'Hanlon24-May-11 1:03 
GeneralRe: Using Wild Character _ Pin
Luc Pattyn24-May-11 1:20
sitebuilderLuc Pattyn24-May-11 1:20 
AnswerRe: Using Wild Character _ Pin
Mycroft Holmes23-May-11 14:07
professionalMycroft Holmes23-May-11 14:07 
GeneralRe: Using Wild Character _ Pin
Member 455049324-May-11 0:18
Member 455049324-May-11 0:18 
GeneralRe: Using Wild Character _ Pin
Pete O'Hanlon24-May-11 0:35
mvePete O'Hanlon24-May-11 0:35 
GeneralRe: Using Wild Character _ Pin
Pete O'Hanlon24-May-11 0:41
mvePete O'Hanlon24-May-11 0:41 
AnswerRe: Using Wild Character _ PinPopular
PIEBALDconsult23-May-11 14:30
mvePIEBALDconsult23-May-11 14:30 
QuestionHow to read the splitcontainers Programmatically ? Pin
Paramu197323-May-11 0:11
Paramu197323-May-11 0:11 
AnswerRe: How to read the splitcontainers Programmatically ? Pin
Richard MacCutchan23-May-11 1:21
mveRichard MacCutchan23-May-11 1:21 
GeneralRe: How to read the splitcontainers Programmatically ? Pin
Paramu197323-May-11 2:14
Paramu197323-May-11 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.