does string value from excel Interop need to be released?

Add-in Express™ Support Service
That's what is more important than anything else

does string value from excel Interop need to be released?
 
Ming Chao




Posts: 20
Joined: 2019-01-23
hi,

i have read an article By Pieter van der Westhuizen how to properly release Excel COM objects but didn't mention other object types like string in C#. In my code, i attempted to save the the cell address to a string object. will this line of code cause memory leak if cellAddr is not being released.

string cellAddr = cell.get_Address(false, false);
Posted 17 Oct, 2019 07:51:47 Top
Andrei Smolin


Add-in Express team


Posts: 16673
Joined: 2006-05-11
Hello Ming,

No. The problem with COM objects is: they don't release in the way .NET objects (including strings) get released. You can expect that you receive a COM object if you call a property or method *returning an object*, not a simple type (such as a string, number, Boolean or enum value). In 99.99% cases, such an object would be a COM object; the remaining cases are absolutely rare; I can't remember if they exist in the Excel object model. To be absolutely sure, you can use Marshal.IsComObject to check this.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 17 Oct, 2019 09:11:57 Top
Ming Chao




Posts: 20
Joined: 2019-01-23
Thank you Andrei for your insights. they are very helpful.
Posted 17 Oct, 2019 09:38:56 Top
Andrei Smolin


Add-in Express team


Posts: 16673
Joined: 2006-05-11
You are welcome!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 17 Oct, 2019 09:46:36 Top