Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I destroy COM objects in C#?

Tags:

c#

com

vsto

What are the methods to destroy Excel COM Interop objects in C#, besides these:

object_instance = null;
System.GC.collect();
&
System.Runtime.InteropServices.Marshal.ReleaseComObject(object);

Please suggest only inbuilt techniques, not other tools like bigcannon, etc.

like image 512
perilbrain Avatar asked Mar 15 '10 16:03

perilbrain


People also ask

How do you destroy an object in CPP?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .

Which function is used to destroy object?

Just as constructor functions create objects, destructor functions destroy objects.

Does deleting a pointer delete the object?

delete keyword in C++Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed. The delete operator has void return type does not return a value.

How can a program destroy an object it has created?

Question: How does a program destroy an object that it creates? Answer: A program does not explicitly destroy objects. A program can set all references to an object to null so that it becomes eligible for garbage collection. But the program does not actually destroy objects.


3 Answers

The kicker is that if you haven't dropped all references to the object, even GC.Collect won't destroy it.

The rule in C# (or .NET generally) is that you can't destory an object. Dispose() won't do it. A finalizer won't do it (rule number 2, don't use a finalizer unless you know why you need it, and don't ever call it directly).

For .NET, these are the things you need to do:

  1. If an object holds references to unmanaged objects, implement IDispose pattern. And implement it fully; it's more than just providing a Dispose() method.
  2. The only reason to have a finalizer is to release unmanaged objects, and only in the event that your object hasn't been properly disposed. Once your dispose method is called, it should do the cleanup, then "kill" the finalizer by calling GC.SuppressFinalize.
  3. If you are using an object that implements a Dispose() method, call Dispose() when you're done with that object. Best practices is to allocate that object with a using block. Exit from the using block will call Dispose() automatically.
  4. When you are done with an object, drop all references to the object, including event handlers, delegates, etc.
  5. Beyond that, trust the garbage collector to do its job. Don't mess with the garbage collector; you're only likely to make things worse, unless you really, really, really know what you're doing and why.
like image 183
Cylon Cat Avatar answered Oct 20 '22 04:10

Cylon Cat


The garbage collector is the only mechanism that can destroy a managed object, but you usually don't invoke it explicitly. You just let it do its thing.

Just like you never take your own trash to the depot, you just leave it sitting on the corner. It's always the garbage man's responsibility.

You can release references to things and clean them up with IDisposable, finalizers and destructors but not destroy them.

By using System.GC you can ask the garbage man to do things early - request a custom run just for yourself - but this usually screws up his schedule and he has a lot more trash to deal with than just yours so it's not recommended.

like image 29
John K Avatar answered Oct 20 '22 02:10

John K


For the most part, you have to remove all references to an object. Only then will the garbage collector see it and destroy it.

Be aware of this:

object_instance = null;

All this does is kill the object_instance reference. This works if it's the only reference. If there are other references, then it won't be collected.

var skywalker = new Person();
var object_instance = skywalker;

...

object_instance = null;
//It's still alive and won't be collected because skywalker lives...
like image 26
Aaron Daniels Avatar answered Oct 20 '22 02:10

Aaron Daniels