Skip to content

Commit

Permalink
Fix for leaking event args. (#41)
Browse files Browse the repository at this point in the history
Adding code to cleanup event args when no handler is registered.
Releasing audio config properly.
  • Loading branch information
glecaros authored Aug 5, 2021
1 parent 055eade commit cad535f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 82 deletions.
1 change: 1 addition & 0 deletions audio/audio_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (config AudioConfig) GetHandle() common.SPXHandle {
// Close releases the underlying resources
func (config AudioConfig) Close() {
config.properties.Close()
C.audio_config_release(config.handle)
}

func newAudioConfigFromHandle(handle C.SPXHANDLE) (*AudioConfig, error) {
Expand Down
41 changes: 16 additions & 25 deletions dialog/callback_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package dialog

import (
"github.com/Microsoft/cognitive-services-speech-sdk-go/speech"
"sync"

"github.com/Microsoft/cognitive-services-speech-sdk-go/speech"
)

// #include <speechapi_c_common.h>
// #include <speechapi_c_recognizer.h>
// #include <speechapi_c_dialog_service_connector.h>
import "C"

var mu sync.Mutex
Expand All @@ -29,11 +32,9 @@ func getSessionStartedCallback(handle C.SPXHANDLE) speech.SessionEventHandler {
//export dialogFireEventSessionStarted
func dialogFireEventSessionStarted(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSessionStartedCallback(handle)
if handler == nil {
return
}
event, err := speech.NewSessionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -56,11 +57,9 @@ func getSessionStoppedCallback(handle C.SPXHANDLE) speech.SessionEventHandler {
//export dialogFireEventSessionStopped
func dialogFireEventSessionStopped(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSessionStoppedCallback(handle)
if handler == nil {
return
}
event, err := speech.NewSessionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -83,11 +82,9 @@ func getRecognizedCallback(handle C.SPXHANDLE) speech.SpeechRecognitionEventHand
//export dialogFireEventRecognized
func dialogFireEventRecognized(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getRecognizedCallback(handle)
if handler == nil {
return
}
event, err := speech.NewSpeechRecognitionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -110,11 +107,9 @@ func getRecognizingCallback(handle C.SPXHANDLE) speech.SpeechRecognitionEventHan
//export dialogFireEventRecognizing
func dialogFireEventRecognizing(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getRecognizingCallback(handle)
if handler == nil {
return
}
event, err := speech.NewSpeechRecognitionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -137,11 +132,9 @@ func getCanceledCallback(handle C.SPXHANDLE) speech.SpeechRecognitionCanceledEve
//export dialogFireEventCanceled
func dialogFireEventCanceled(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getCanceledCallback(handle)
if handler == nil {
return
}
event, err := speech.NewSpeechRecognitionCanceledEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -164,11 +157,9 @@ func getActivityReceivedCallback(handle C.SPXHANDLE) ActivityReceivedEventHandle
//export dialogFireEventActivityReceived
func dialogFireEventActivityReceived(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getActivityReceivedCallback(handle)
if handler == nil {
return
}
event, err := NewActivityReceivedEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.dialog_service_connector_activity_received_event_release(handle)
return
}
handler(*event)
Expand Down
86 changes: 30 additions & 56 deletions speech/callback_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

// #include <speechapi_c_common.h>
// #include <speechapi_c_recognizer.h>
// #include <speechapi_c_synthesizer.h>
import "C"

var mu sync.Mutex
Expand All @@ -28,11 +30,9 @@ func getSessionStartedCallback(handle C.SPXHANDLE) SessionEventHandler {
//export recognizerFireEventSessionStarted
func recognizerFireEventSessionStarted(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSessionStartedCallback(handle)
if handler == nil {
return
}
event, err := NewSessionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -55,11 +55,9 @@ func getSessionStoppedCallback(handle C.SPXHANDLE) SessionEventHandler {
//export recognizerFireEventSessionStopped
func recognizerFireEventSessionStopped(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSessionStoppedCallback(handle)
if handler == nil {
return
}
event, err := NewSessionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -82,11 +80,9 @@ func getSpeechStartDetectedCallback(handle C.SPXHANDLE) RecognitionEventHandler
//export recognizerFireEventSpeechStartDetected
func recognizerFireEventSpeechStartDetected(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSpeechStartDetectedCallback(handle)
if handler == nil {
return
}
event, err := NewRecognitionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -109,11 +105,9 @@ func getSpeechEndDetectedCallback(handle C.SPXHANDLE) RecognitionEventHandler {
//export recognizerFireEventSpeechEndDetected
func recognizerFireEventSpeechEndDetected(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSpeechEndDetectedCallback(handle)
if handler == nil {
return
}
event, err := NewRecognitionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -136,11 +130,9 @@ func getRecognizedCallback(handle C.SPXHANDLE) SpeechRecognitionEventHandler {
//export recognizerFireEventRecognized
func recognizerFireEventRecognized(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getRecognizedCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechRecognitionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -163,11 +155,9 @@ func getRecognizingCallback(handle C.SPXHANDLE) SpeechRecognitionEventHandler {
//export recognizerFireEventRecognizing
func recognizerFireEventRecognizing(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getRecognizingCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechRecognitionEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -190,11 +180,9 @@ func getCanceledCallback(handle C.SPXHANDLE) SpeechRecognitionCanceledEventHandl
//export recognizerFireEventCanceled
func recognizerFireEventCanceled(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getCanceledCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechRecognitionCanceledEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.recognizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -217,11 +205,9 @@ func getSynthesisStartedCallback(handle C.SPXHANDLE) SpeechSynthesisEventHandler
//export synthesizerFireEventSynthesisStarted
func synthesizerFireEventSynthesisStarted(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSynthesisStartedCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechSynthesisEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.synthesizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -244,11 +230,9 @@ func getSynthesizingCallback(handle C.SPXHANDLE) SpeechSynthesisEventHandler {
//export synthesizerFireEventSynthesizing
func synthesizerFireEventSynthesizing(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSynthesizingCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechSynthesisEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.synthesizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -271,11 +255,9 @@ func getSynthesisCompletedCallback(handle C.SPXHANDLE) SpeechSynthesisEventHandl
//export synthesizerFireEventSynthesisCompleted
func synthesizerFireEventSynthesisCompleted(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSynthesisCompletedCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechSynthesisEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.synthesizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -298,11 +280,9 @@ func getSynthesisCanceledCallback(handle C.SPXHANDLE) SpeechSynthesisEventHandle
//export synthesizerFireEventSynthesisCanceled
func synthesizerFireEventSynthesisCanceled(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSynthesisCanceledCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechSynthesisEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.synthesizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -325,11 +305,9 @@ func getSynthesisWordBoundaryCallback(handle C.SPXHANDLE) SpeechSynthesisWordBou
//export synthesizerFireEventWordBoundary
func synthesizerFireEventWordBoundary(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSynthesisWordBoundaryCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechSynthesisWordBoundaryEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.synthesizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -352,11 +330,9 @@ func getSynthesisVisemeReceivedCallback(handle C.SPXHANDLE) SpeechSynthesisVisem
//export synthesizerFireEventVisemeReceived
func synthesizerFireEventVisemeReceived(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSynthesisVisemeReceivedCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechSynthesisVisemeEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.synthesizer_event_handle_release(handle)
return
}
handler(*event)
Expand All @@ -379,11 +355,9 @@ func getSynthesisBookmarkReachedCallback(handle C.SPXHANDLE) SpeechSynthesisBook
//export synthesizerFireEventBookmarkReached
func synthesizerFireEventBookmarkReached(handle C.SPXRECOHANDLE, eventHandle C.SPXEVENTHANDLE) {
handler := getSynthesisBookmarkReachedCallback(handle)
if handler == nil {
return
}
event, err := NewSpeechSynthesisBookmarkEventArgsFromHandle(handle2uintptr(eventHandle))
if err != nil {
if err != nil || handler == nil {
C.synthesizer_event_handle_release(handle)
return
}
handler(*event)
Expand Down
3 changes: 3 additions & 0 deletions speech/session_event_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import "C"

// SessionEventArgs represents the session event arguments.
type SessionEventArgs struct {
handle C.SPXHANDLE
// SessionID Session identifier (a GUID in string format).
SessionID string
}

// Close releases the underlying resources.
func (event SessionEventArgs) Close() {
C.recognizer_event_handle_release(event.handle)
}

// NewSessionEventArgsFromHandle creates the object from the handle (for internal use)
Expand All @@ -32,6 +34,7 @@ func NewSessionEventArgsFromHandle(handle common.SPXHandle) (*SessionEventArgs,
return nil, common.NewCarbonError(ret)
}
event := new(SessionEventArgs)
event.handle = uintptr2handle(handle)
event.SessionID = C.GoString((*C.char)(buffer))
return event, nil
}
Expand Down
2 changes: 1 addition & 1 deletion speech/speech_recognition_event_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type SpeechRecognitionEventArgs struct {

// Close releases the underlying resources
func (event SpeechRecognitionEventArgs) Close() {
event.RecognitionEventArgs.Close()
event.Result.Close()
C.recognizer_event_handle_release(event.handle)
}

// NewSpeechRecognitionEventArgsFromHandle creates the object from the handle (for internal use)
Expand Down

0 comments on commit cad535f

Please sign in to comment.