Visio VSTO Addin persisting objects in memory

Started by Visisthebest, May 30, 2020, 01:20:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

Visio VSTO Addins are a new subject to me, can a Visio VSTO Addin persist objects in memory that remain there until Visio is closed (or the Addin cleans them up after catching the event that the user has closed the page with the diagram for instance)?



Visio 2021 Professional

Paul Herber

Yes, certainly, it's more down to the memory model used by the programming language (C#, C++ etc). Any global (i.e. outside of a class instantiation), static memory item can be used in this manner.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Visisthebest

Good news :) so this is a significant advantage compared to VBA, I understand after a VBA Macro is executed a subsequent macro cannot acces objects created by the macro that ran before it.
Visio 2021 Professional

Nikolay

VBA macro is the same...
If you declare a variable outside the macro, it's value is preserved between calls:


Dim myGlobalVar

Sub Macro1
...
End Sub


Visisthebest

Wow Nikolay thank you I did not know this, very useful if state is properly checked/managed of these persistent variables like arrays. Rebuilding a representation in-memory of the diagram on the page every time before a user performs an action can make the user experience sluggish.
Visio 2021 Professional

vojo

depending on VBA environment settings, you may have to declare variable global and/or use the global key word

public = global  means visible across all applications, modules, macros, functions
Private              means visible to all macros or functions within that given module

explicit             means you have to actually declare the variable before using it (tedious but helps any scope intent)

Visisthebest

Thank you Vojo I always choose option explicit as it is a best practice.

If I create an initialization sub that redims a public/private array variable declared at the beginning of a module (outside any sub/function), that Array will be available to any sub/function in that module that is called after that I understand.

This all requires some good state management code of course, but it solves a lot of problems with performance!
Visio 2021 Professional

Visisthebest

If I declare a public (global) collection object and then store (references) to other collection objects in that collection, do these collection objects that are declared in a sub/function (they are dynamic in nature, I do not know how many at design time only at run time) persist between VBA calls (a global collection still keeps pointing to them after all, so maybe garbage collection doesn't remove them)?

Thank you for your help!
Visio 2021 Professional

Nikolay

There is no garbage collection in VBA, so objects are not auto-deleted.
They can be only deleted by your code.

Visisthebest

Ok so there is some risk of memory leaks with VBA code I wasn't really mindful of this.
Visio 2021 Professional

Nikolay

Although VBA does not use garbage collection, it does uses reference counting.
Like, it counts the number of references to the object, and as soon as it reaches zero, the object is deleted.

What I mean by "deleted by your code" is that unlike C#, in VBA, you can control explicitly when the object is deleted.

An example:


Sub Foo
  Set x = new Dictionary
End Sub '<<< dictionary object IS deleted on exiting the sub, as the last reference to it is gone



Set Foo2
  Set x = new Dictionary
  y =10
  Set x = Nothing '<<< dictionary object IS deleted here
  y = 20
End Sub



Set Foo3
  Set x = new Dictionary
  Set z = x
  y = 10
  Set x = Nothing
  y = 20
End Sub '<<< dictionary object IS deleted here, because there is z that still keeps the reference to it



Dim z

Set Foo4
  Set x = new Dictionary
  Set z = x
End Sub '<<< the dictionary is NOT deleted on exit of the sub, because global z still keeps a reference to it


Visisthebest

Thank you will do some tests with this, I assume if a global Collection object holds a Collection object declared in a Sub that holds another Collection object declared in a Sub (etc), that everything is persisted because of reference counting. Very useful this can make VBA a lot faster in several scenarios.
Visio 2021 Professional