Search code examples
vb.netdisposelocal-variables

Is Static local variable in vb.net unsuitable for disposable objects?


Are local Static variables a no-go for disposable objects? The main problem with this case: Dispose() might never get called.

Example:

Sub DrawText(text As String, fontFamily As Font)
    Static cachedFont As Font = Nothing
    If cachedFont Is Nothing OrElse fontFamily <> cachedFont.Family Then
        cachedFont = New Font(fontFamily)
    EndIf
    'now draw text using cachedFont
End Sub

Is such cases, is the only way to convert local static variable into local class variable (which is being disposed in Sub Dispose(disposing As Boolean))?


Solution

  • Yes, they are a no-go if you want them to be disposed.

    Static local variables are compiled to

    • in a Shared procedure:
      • to Shared field variables at class level
    • in a non-Shared instance method:
      • to instance field variables at class level

    ...which name is derived from the method name and the variable name to ensure that it's unique per type.

    The Shared variables are never be disposed for the lifetime of an application because you can dispose only instances. The instance variables are disposed when you call instance.Dispose or use the Using-statement.

    But the garbage collector in the CLR does not (and cannot) dispose of unmanaged objects. So the problem is that you can't dispose these objects from Dispose because it's out of scope as local variable. So if you need to clean up unmanaged resources in Dispose you can't do that with Static local variables because you can't access them.

    Worth reading: