0

I have a class that I need in many forms, so I have declared it in a module.

Module modTTS

    Public g_TTS As clsTTS

End Module

I have a Finalize method in this class:

Protected Overrides Sub Finalize()

    Stop'this is never called. I don't know why

    MyBase.Finalize()

End Sub

I do not explicitely destroy the class when my program ends. I just wanted to silently let it slip out of scope. Is that not valid? What might be the reason why the Finalize procedure is not called?

Thank you!

According to the suggestions, I have changed my class to this:

Public Class clsTTS : Implements IDisposable

Private _Synth As New SpeechSynthesizer
Private _bDisposed As Boolean = False

' Public implementation of Dispose pattern callable by consumers. 
Public Sub Dispose() Implements IDisposable.Dispose

    Dispose(True)
    GC.SuppressFinalize(Me)

End Sub

' Protected implementation of Dispose pattern. 
Protected Overridable Sub Dispose(ByVal uDisposing As Boolean)

    If _bDisposed Then
        Return
    End If

    If uDisposing Then
        ' Free any other managed objects here. 
        If Not _Synth Is Nothing Then
            _Synth.Dispose()
        End If
    End If

    ' Free any unmanaged objects here. 
    _bDisposed = True

End Sub
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • `GC.SuppressFinalize(Me)` in Dispose well..supresses teh finalize event (see http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize(v=vs.110).aspx). Finalize is for unmanaged resources, and IDisposable for managed resources. if/when you have both, perform everything in Dispose. CA/Fx will help identify the things too. – Ňɏssa Pøngjǣrdenlarp Jun 03 '14 at 23:29

2 Answers2

3

here's an example using the ApplicationContext Class - MSDN

also, Finalize() might not be called before the application thread terminates, therefore, resources are never released.

You should implement the IDisposable Interface - MSDN instead. and call .Dispose() method of your object on ApplicationExit.

iDisposable is simple to implement, just add : Implements iDisposable to your Class declaration, and intellisense will add the necessary procedures.

You can also dispose of /close objects in a FormClosing / FormClosed event.

personally, i only use Shared members as functions, or to keep some string vars, but not objects.

porkchop
  • 401
  • 3
  • 8
  • Thank you. Don't you have any global objects? – tmighty Jun 03 '14 at 22:06
  • I guess I need to add the "Close" proc myself in the class. What should I do in "Close"? Should I call "Me.Dispose"??? – tmighty Jun 03 '14 at 22:10
  • oh sorry, you can just call `.Dispose()`. `.Close()` basically calls `.Dispose(True)` for most .net classes. and release everything in `If disposing Then` block. here is an example: http://msdn.microsoft.com/en-us/library/b1yfkh5e%28v=vs.110%29.aspx heh they just renamed `.Dispose` ;p – porkchop Jun 03 '14 at 22:12
  • I have changed my post accordingly. Could you have a look at it and tell me if it is correct in your eyes? Thank you! – tmighty Jun 03 '14 at 22:19
  • 1
    @tmighty it looks correct. VS2010 syntax. also, just discovered you can use `iDisposable` with `Shared` classes (shared CTOR), but i'm not sure exactly what the implications of that would be just yet. i've always used it with non-shared CTORs, with possibly some shared members. – porkchop Jun 03 '14 at 22:27
  • @tmighty actually, that breaks down when you try to declare `Dispose()` as `Shared` since it's an interface. i actually cheated and just 'made' a `Dispose` function lol. although i should probably fix that... should instead create a `Shared` member somewhere that is a new instance of that class. ie: `Friend Shared translator As New translatorClass` in some 'common' class, then `common.translator.Dispose()`. – porkchop Jun 03 '14 at 23:05
0

Take a look at the following link to learn how to do unintialization of static members:

http://bytes.com/topic/c-sharp/answers/536465-there-anything-like-static-destructor

How to get notified before static variables are finalized

Community
  • 1
  • 1
matra
  • 9,763
  • 4
  • 20
  • 31