3

I get a CA2000 error from my code analysis asking me to dispose my objects before exiting the scope

Dispose objects before losing scope In method 'MyMethod(Guid?)', call System.IDisposable.Dispose on object 'person' before all references to it are out of scope.

I'm surprised of this rule because I thought the dispose was automatically run when exiting the scope. Note : A similar question has already been asked and confirm my opinion to not force the dispose procedure.

What happens if I don't dispose my object and i don't use it in a using instruction?

Community
  • 1
  • 1
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
  • Where did you get that quote? Out of context, it doesn't seem to mean much; just wrap your method in a `using` statement. – Robert Harvey Dec 13 '12 at 15:32
  • Post your code. Only using `using` will call dispose. – Miserable Variable Dec 13 '12 at 15:33
  • It depends on the definition of Person. That it implements IDisposable suggests it should be in a using block. – Kaido Dec 13 '12 at 15:34
  • Yes Person implements IDisposable. But what if I dont write the using or dispose my objet. Will my object live forever in the memory ? – Bastien Vandamme Dec 13 '12 at 15:36
  • 1
    Why "of course". Is there something in it that REALLY needs disposal? And Dispose will NOT be called automatically. The only thing called automatically is the finalizer (if implemented) and that will `usually` call dispose. But only when the GC starts a collection - which might be NEVER or OFTEN during your app's lifetime. – igrimpe Dec 13 '12 at 15:39
  • @MiserableVariable: Actually, you can call dispose on a class that implements `IDisposable` anytime you want to. The `using` statement is the way to go, but if for whatever reason someone did not want to take advantage of compiler magic they could simply use a `try { ... } finally { //Dispose }` ... That's what using statements compile into anyways. – myermian Dec 13 '12 at 15:46
  • @m-y I should have said `using` will call it automatically, at the end of the block. Though I would not call `dispose` in a `using` block myself – Miserable Variable Dec 13 '12 at 16:20

4 Answers4

4

Dispose is never called automatically. You must call it explicitly, or use the 'using' keyword At least, if you don't dispose your object, it may be done by the garbage collector but only if you implement the correct patern.

Cédric Rup
  • 15,468
  • 3
  • 39
  • 30
3

If you have wrapped your object inside the using statement you are ok, because it will be translated to.

try{
    var myObject = new MyObject();
}finally{
    myObject.Dispose();
} 

Update

If you do not dispose your object it 'might stay in memory forever' keeping the attached resources. But might not. It depends on garbage collector when it comes around tries to clear it. But beware GC is only able to clear managed resources.

Look here for similar question: What happens if I don't call Dispose on the pen object?

Community
  • 1
  • 1
mipe34
  • 5,596
  • 3
  • 26
  • 38
2

I thought the dispose was automatically run when exiting the scope.

Not any scope, only a using block scope. Exiting a method is not the same as exiting a using block.

If an object implements IDisposable, then you should always dispose it manually or via a using block. It is up to you when to do this, based on the structure of your code.

Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
1

Dispose not called when object goes out of scope. In many scenarios it will be called when garbage collect the object (when implemented Finalizer and Dispose called in it).

Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68