3

In the following, I'm passing a disposable object as a parameter in a constructor, then doing a few operations and that's it. The constructor does nothing other than set a local variable instance of DisposableObject.

Am i right in believing the disposable will still get disposed even in the result of an exception in the Whatever() call?

If the code was in a for loop both objects would also go out of scope and get garbage collected as well (I can only have one instance of this IDisposable class in my App Domain).

using (var disposableObject = new DisposableObject()) { var simpleObject= new Simple(disposableObject); simpleObject.Whatever(); } 
4
  • 2
    Yes, the instance will be disposed if disposableObject has been created. Commented Oct 22, 2018 at 12:02
  • "I can only have one instance of this IDisposable class in my App Domain" This seems like a strange design choice. Commented Oct 22, 2018 at 12:03
  • We can't guarantee that GC (garbage collector) will run (let alone collect the instance) Commented Oct 22, 2018 at 12:03
  • @Zahar Peled, I agree but its a 3rd party object Ive no control over Commented Oct 22, 2018 at 12:19

1 Answer 1

2

Just to clarify: IDisposable isn't about garbage collection, but instead about the larger topic of resource management. A simple example might be a SqlConnection which is disposed to release the connection to the database. The SqlConnection object still lives in memory and is garbage collected after it has fallen out of scope.

This won't help with your "one-instance-per-AppDomain" problem, since the lifetime of the object is not controlled by you. At best you can have one non-disposed object.


A using statement is effectively a try-finally block, where the finally always runs to make sure the object is disposed.

This code:

using(var disposable = new DisposableObject()) { ... } 

essentially is the equivalent to this code:

var disposable = new DisposableObject(); try { ... } finally { disposable?.Dispose(); } 

Dispose is always called, unless the reference to the object is lost.

Sign up to request clarification or add additional context in comments.

3 Comments

What are you thinking might allow the reference to be "lost"? disposable (in your example) is read-only within the body of the using, so it's not like it can be assigned null.
Thanks Paul. Yeah didnt mean to muddy waters with the GC added in there. The DisposableObject class is in reality a 3rd party class which throws an exception claiming there acant be more than one instance.I wanted to be 100% I was doing legit code. I think there something going one with the 3rd party class thats nothing to do with above code. (I also need to recreate the class multiple time for reason irrelevant to the question)
@Damien_The_Unbeliever In the case of the try-finally, if you were to assign a new value to the disposable field, the original value is lost and cannot be disposed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.