Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COM interop question

Tags:

c#

interop

com

Am using COM interop in my C# application.

I've this line of code:

IMyInterface objData = MyCOMClass.GetData();

My question here is:

Do I need to release resources on objData by using? System.Runtime.InteropServices.Marshal.ReleaseComObject(objData);

Thanks for reading.

like image 721
Jimmy Avatar asked Dec 20 '25 17:12

Jimmy


1 Answers

Yes, unless it is ok for you to wait until the GC cleans it up.

Could be worth noting that the COM interop creates one COM reference per Interface.

IMyInterface x = MyCOMClass.GetData();
IMyOtherInterface y = (IMyOtherInterface)x;
IMyInterface z = x;

Marshal.ReleaseComObject(x);
Marshal.ReleaseComObject(y);

or Marshal.FinalReleaseComObject(x); // If you know nobody else uses it

like image 106
adrianm Avatar answered Dec 22 '25 05:12

adrianm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!