Correct way to release a COM object

Our ActiveTTS component is a COM component. Using it in .Net needs some attention on how to release it.

.Net uses GC to release unused object. But for COM object, it is better to release it as soon as possible when it is not used.

The following example code shows how you release ActiveTTS object when it is done.

       Dim i
        For i = 0 To 1000
                Dim objActiveTTS As New TTSObj.CTextToSpeechClass
                Dim strCode As String
                objActiveTTS.SpeakToFile(“Hello world”, “d:tempxxxx.mp3″, TTSObj.TTSFLAG.TF_DEFAULT)
            Catch ex As Exception
            End Try

Marshal.ReleaseComObject is the key method.