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
            Try
                Dim objActiveTTS As New TTSObj.CTextToSpeechClass
                Dim strCode As String
                objActiveTTS.SetCode(strCode)
                objActiveTTS.SpeakToFile(“Hello world”, “d:tempxxxx.mp3″, TTSObj.TTSFLAG.TF_DEFAULT)
                Marshal.ReleaseComObject(objActiveTTS)
            Catch ex As Exception
            End Try
        Next

Marshal.ReleaseComObject is the key method.