Visio Guy

Visio Discussions => Programming & Code => Topic started by: Visisthebest on May 30, 2020, 01:20:37 PM

Title: Visio VSTO Addin persisting objects in memory
Post by: Visisthebest on May 30, 2020, 01:20:37 PM
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)?



Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Paul Herber on May 30, 2020, 01:33:00 PM
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.
Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Visisthebest on May 30, 2020, 02:55:12 PM
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.
Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Nikolay on May 30, 2020, 05:06:52 PM
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

Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Visisthebest on May 30, 2020, 06:55:48 PM
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.
Title: Re: Visio VSTO Addin persisting objects in memory
Post by: vojo on May 30, 2020, 10:26:25 PM
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)
Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Visisthebest on May 31, 2020, 07:55:37 AM
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!
Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Visisthebest on June 08, 2020, 09:57:23 AM
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!
Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Nikolay on June 08, 2020, 10:49:14 AM
There is no garbage collection in VBA, so objects are not auto-deleted.
They can be only deleted by your code.
Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Visisthebest on June 08, 2020, 01:11:26 PM
Ok so there is some risk of memory leaks with VBA code I wasn't really mindful of this.
Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Nikolay on June 08, 2020, 08:52:16 PM
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

Title: Re: Visio VSTO Addin persisting objects in memory
Post by: Visisthebest on June 09, 2020, 08:58:27 AM
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.