Release Objects

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

Release Objects
which Objects need to be released 
Michael Kaden




Posts: 113
Joined: 2017-11-15
Dear Andrei,

This is a more general Question, related to robust programming, however I would very much appreciate your clarification.

If I for example use:

Dim wSheets As Excel.Sheets = xlBook.Worksheets
Dim wks As Excel._Worksheet = CType(wSheets.Item(iSheet), Excel._Worksheet)

I would use at the end of the procedure:

Marshal.ReleaseComObject(wks) : wks = Nothing

should I also use?

Marshal.ReleaseComObject(wSheets) : wSheets = Nothing

A more difficult one for me is if I use "as New"

For example

Dim serv As New Service - where Service is a Function in another class = "Utilities"

( I am aware that I could use Utilities.CurrentInstance.Service() but I want deliberately create a new instance of Function Service())

or to open a Form

Dim NewForm As New OldForm - where OldForm is a Windows Form

Do I have to release serv or NewForm and if yes, then how and where, perhaps at the end of the procedure utilising the newly created class with ?

Marshal.ReleaseComObject(serv) : serv = Nothing
Marshal.ReleaseComObject(NewForm) : NewForm = Nothing

Hope the above questions are understandable and not too stupid.


thank you & regards

Michael
Posted 06 Mar, 2018 07:14:16 Top
Andrei Smolin


Add-in Express team


Posts: 14809
Joined: 2006-05-11
Hello Michael,

When you deal with a COM collection, you create a COM object representing the collection object plus a COM object for every item in the collection. When you're done with the collection all of these COM objects should be released: the collection object and every item object.

Is a service instance a COM object? I suppose no. Is a form instance a COM object? I suppose no. You can verify this using Marshal.IsComObject

When you deal with any object model in Office, every object type in the object model is a COM object. Outside of Office, well, it depends on the library/component that you use.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 06 Mar, 2018 07:26:44 Top
Michael Kaden




Posts: 113
Joined: 2017-11-15
Thank you Andrei,

your pointer to Marshal.IsComObject answered my query.

thank you & regards

Michael
Posted 06 Mar, 2018 08:14:59 Top
Andrei Smolin


Add-in Express team


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

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 06 Mar, 2018 08:21:43 Top
Androulla Pavlou




Posts: 10
Joined: 2016-05-25
I'd like to add to this question, if I may.

I did some reading a few months back on this and it *seemed* as though this releasing of objects was only recommended in the case of VBIDE add-ins, but not necessary with regular Excel COM add-ins. Am I mistaken? Should I be releasing all objects in my Excel COM add-in code?

Tim Jeffryes
TJs Tools LLC
www.tjs-tools.com
Posted 06 Mar, 2018 11:42:47 Top
Andrei Smolin


Add-in Express team


Posts: 14809
Joined: 2006-05-11
Hello Tim,

This doesn't depend on whether you use the Excel object model or VBIDE: all COM objects should be released.

The situation becomes difficult if we talk about several consumers that use COM classes from the same object model in the same Appdomain.

If the add-in are loaded in *separate* AppDomains (this is always the case with Add-in Express based add-ins loaded using adxloader.dll/adxloader64.dll), releasing such a COM object by one add-in doesn't influence the other add-in.

If however the add-ins are loaded in the *same* AppDomain, releasing a COM object may influence the other add-in if it uses the same COM object. If you use FinalReleaseComObject, influencing the other add-in loaded in the same Appdomain is almost inevitable since you release a COM object that the other add-in may rely on.

Feel free to ask me if you have any questions or need more information.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Mar, 2018 04:18:05 Top
Michael Kaden




Posts: 113
Joined: 2017-11-15
Hello Andrei,

interesting points. However I would like to briefly come back to my other point in my query.

Assuming I have:

Class1
Function A (K as integer)as integer
Dim L as integer = 3
A = L +K
End Function
End Class

Class2
Function B (M as integer)as integer
Dim NewFIns as New A
Dim N as integer = 4
Dim O as integer
O = NewFIns(N)
B = O + N
End Function

somewhere else in this class

XYZ = B(5)

End Class

I assume NewFins is a newly created object?

after YXZ is executed, I assume that the memory space occupied by

K,L,M,N,O as well as Functions A,B,NewFIns is released, without any other action such as Dispose Method etc.?

Am I correct there?

I assume that NewFIns is an object?

Sorry this is not really a ADX question, but I have difficulties to find conclusive answers anywhere, so your insight is very much appreciated.

Ps I have read

MSD Object Lifetime

but this added more confusion than clarification.

thank you & regards

Michael
Posted 07 Mar, 2018 07:15:22 Top
Andrei Smolin


Add-in Express team


Posts: 14809
Joined: 2006-05-11
Michael Kaden writes:
I assume NewFins is a newly created object?


Yes. This is a delegate pointing to a function, I assume?

Michael Kaden writes:
K,L,M,N,O as well as Functions A,B,NewFIns is released, without any other action such as Dispose Method etc.?


They are released and the memory they used is marked as free after the Garbage Collector (GC) is run and found them unused (=non-referenced); GC collects such memory and marks it free. That is, there's some time between stopping using a variable and getting its memory freed.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Mar, 2018 07:23:17 Top
Michael Kaden




Posts: 113
Joined: 2017-11-15
Thank you
Posted 07 Mar, 2018 09:06:13 Top