4

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))?

8
  • In my opinion, I'd put it as a member on the class anyway, for clarity, disposable or not. Sep 4, 2015 at 10:28
  • 1
    @JamesThorpe – clarity can be discussed. Sometimes is it better to have trivial variable in one place than to see it scattered around the class (constructor + method where it is needed + disposer).
    – miroxlav
    Sep 4, 2015 at 10:30
  • Indeed - hence my opinion :) If it's something that lives with the class, it deserves to be on the class. Sep 4, 2015 at 10:31
  • 1
    Blindly micro-optimizing code is never not a mistake. Your static variable accomplishes nothing, fonts are already cached. Use the Using statement. Sep 4, 2015 at 11:04
  • @HansPassant – your note that fonts are already cached helped for that specific case. I couldn't see it in .net docs. Are they cached in Windows or in .net? And yes, Using statement is what I'm actually using at the moment.
    – miroxlav
    Sep 4, 2015 at 11:09

2 Answers 2

2

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:

5
  • Maybe you can correct last paragraph: Static and Shared are not equal in their scope. Only Shared value is one per type. But Static value is one per class instance. You can see it in Behavior pargaph in MSDN article on Static. This might somewhat change also your explanation of what compiler actually does.
    – miroxlav
    Sep 4, 2015 at 10:46
  • @miroxlav: Static(in VB.NET) is the same as Shared but only used for local variables. Yes, they have a different scope. You cannot access a Static variable from outside of the method or from another class. But they are equal after compilation. What happens behind the scenes is that the compiler will create a new static field variable in the type for every Static variable. It's name will be derived from the method-name + variable-name. Sep 4, 2015 at 10:46
  • @miroxlav: Let me read that article first, maybe i was wrong ;) Sep 4, 2015 at 10:48
  • While I understand your explanation, it is only explaining the case of VB Static in Shared procedure. MSDN continues, I quote: When you declare a static variable in a procedure that isn't Shared, only one copy of the variable is available for each instance of the class.. Concept of VB Static is a bit different from what one would intuitively think. So may I assume that Static variable in non-shared sub will be auto-disposed along with its class instance? Hence, it is no big issue in having disposable variable in it? This could change things a little (including the answer).
    – miroxlav
    Sep 4, 2015 at 10:55
  • @miroxlav: yes, i think that's true. All non-shared method's static variables are garbage collected when the instance is no longer refernced and the GC is active. But the problem is that you can't dispose this object from Dispose because it's out of scope as local variable. So if you need to clean up Uumanaged resources in dispose you can't do that with static local variables. Sep 4, 2015 at 11:02
1

Sad to say it, after checking with VS2015's Memory Usage Tool I can confirm that Static instances are NOT GC'ed when closing/disposing for example a form using them.

To test it create a project with two forms. In Form2's Form2_Load event declare and add some fonts to a Static List(Of Font). In Form1_Load create and show an instance of Form2.
Execute the project using Debug->Performance and Diagnostics menu option, use Memory Usage behavior.
You'll get both forms popping up.
Close Form2. Click Force GC button in Memory Usage Tool window and take a memory snapshot.
Close Form1 to stop execution.

Click nnnn objects link in Snapshot 1, a new window will open. Uncheck Collapse small objects in upper right dropdown button of that window.
Type 'font' in search box and you'll see all those Fonts remaining in memory.

1
  • Thank you for your time and effort to test this and share your findings.
    – miroxlav
    Jan 23, 2019 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.