Dispose questions

Maybe this is in the gray book but I couldn’t find a clear answer.

I want to be sure I’m using dispose properly so I need to understand when I can rely on gamma to call it and when I need to manually call dispose.


  • Is pressing F9 in the editor the same dispose behaviour as quitting an exported app?
  • Is pressing F8 in the editor the same dispose behaviour as quitting an exported app?

I put together a test patch in 2022.4.12. It uses WriteLine as @Tonfilm suggested in chat to test dispose. DisposeTest.vl (36.5 KB)

From my tests I came up with the following statements. Are they correct?

  • There is no way to create a dispose operation in the root application patch of a document.
  • Any process node on the root patch will automatically have it’s dispose operation called when the application stops.
  • Any process node inside another object will automatically have it’s dispose operation called when the parent object disposes
  • If a class or record has a dispose operation that will NOT automatically be called when the application stops.
  • Dispose for classes and record must be manually called as part of the dispose operation of a parent object. Because processes are the only nodes where dispose will be automatically called on quit then the top parent in such a ‘dispose stack’ should be a process that is a child of the root patch.

Other questions

  • How does iDisposable fit into this? Is that only for working with external objects that should be disposed? Or is there some way dispose for objects with iDisposable called automatically by gamma editor?
1 Like

All observations are correct. But I wouldn’t use the term “automatically called”, using dispose is only a software pattern. And in VL Create, Update and Dispose are such patterns. If a process node has one of these defined and is put into a stateful patch that also has these, they will be automatically assigned to the ones of the surrounding stateful patch.

The special thing with Dispose is that if a surrounding stateful patch doesn’t have Dispose explicitly defined in the patch explorer, it will create an implicit Dispose as soon as a process node with a Dispose operation is placed into it.

Now if you have dynamic instances of a class (not using a process node, but using its Create operation explicitly) you have to take care of calling Dispose somewhen (if it has it). As mentioned above, a class can have an implicit Dispose operation if you place a Process with Dispose into it. The UI doesn’t show that yet, which leads to a bit of confusion sometimes, I think.

IDisposable is the official .NET interface that defines the Dispose method. VL also uses it everywhere. As soon as a VL type has a Dispose operation it will implement that interface.

There are some things that make working with Dispose easier, for example the “Dispose Cached Output” on the Cache region. It will try to dispose an object stored in its output border control point, if the region is executed, or when the surrounding patch is disposed.

But if you have a Spread or other collection of disposable objects, you have to call them manually. For example with TryDispose, which will try to dispose the object or a collection of objects.


Thanks for all the info!

I had a further complication when I tried this on my real usecase.
My stack of dipose calls was only partially running. It turns out it was blocked by an object not being assigned.

The problem is there is no error feedback, no node turns purple. So took quite a while to find the issue.
(And note I’m using ‘regular’ MyObject.Dispose, not TryDispose)

Is this by design or should I file a bug?

It seems to me like if I press F8 to stop, it runs dispose, and then there is an exception on dispose than I should see a purple node. But I understand there could be a limitation here?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.