views:

320

answers:

5

I was wondering when the destructor is called under these circumstances, and if it is will it be called on the main UI thread?

Let's say I have the following code, when would the destructor be called, and would it wait until I have finished all my function calls?

private void Foo()
  {
  MyObject myObj = new MyObject();
  DoSomeFunThingsWithMyObject(myObj);

  myObj = new MyObject(); //is the destructor for the first instance called now?
  DoLongOminousFunctionality(myObj);  
  }
  //Or will it be called after the DoLongOminousFunctionality?

It's just something that interests me, if the thread is interrupted at myObj = new MyObject(), or if the Destructor call waits until the Thread is free.

Thanks for the information.

+2  A: 

Destructors or finalizers as they are also named are called at some point in time after your instance is available for garbage collection. It doesn't happen at a deterministic point in time as in C++.

Brian Rasmussen
+2  A: 

Destructors (or finalizers are some people prefer to call them) are run on a seperate thread altogether. They are run at no particular time. They aren't guaranteed to run at all till the end of your applications life, and even then it's possible they won't get called.

Matthew Scharley
+13  A: 

Destructor will be called when Garbage collector decides that it have to clean up some old objects. You cannot rely on destructors execution time in .NET

Instead of that you should use Dispose() if you want to clean up some resources when they are not needed (especially when you have any unmanaged resources such as TCP connections, SQL connections etc.)

See Implementing a Dispose Method

Bogdan_Ch
The first sentence could perhaps do with some clarification. When the garbage collector performs a collection, it will add objects with finalizers to the finalization queue. So, the finalizer could be called at any point after the object has initially been garbage collected, not necessarily immediately.
Tim Greaves
(to channel Fowler)-First rule of implementing Dispose:"Don't implement dispose". ;-) Make sure to read the link in this answer. Choosing to implement dispose is non-trivial. Garbage collections means that in most cases we don't need to worry about when the object is destructed.
TheZenker
Well, this rule (do not implement dispose when you dont need to release resources) sometimes ignored for a code simplicity. Have you ever seen that developers are using Dispose() just for simplicity of writing some code that will work with using {new myobject()} statement. For example: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/18b8b65e-03cd-4a4f-9b7e-4e44ea5ac5df . So this code do nothing with resources, it is just a trick of how language feature can be used for a different purpose.
Bogdan_Ch
Here is another rule - "Just Say No to Finalize Methods!" :) http://www.developer.com/net/csharp/article.php/2233111 . However the discussion about what is better could be found in another SO topic http://stackoverflow.com/questions/732864/finalize-vs-dispose
Bogdan_Ch
+3  A: 

If it is crucial that the lifetime of your objects is managed, inherit from IDisposible and you can use the using keyword.

DanDan
+1  A: 

The destructor (finalizer) will be called once the garbage collector can determine that your object is no longer used. Finalizers run on the finalizer thread, concurrently to your main program!

With the right optimizations (the JIT compiler could easily eliminate the local variable), this might be as early as inside the first DoSomeFunThingsWithMyObject call (once that method doesn't need its parameter anymore), or at any later time. Maybe it isn't even called until your program is shut down (and in rare cases, the finalizer is never called).

Daniel