diff --git a/audio/audio_config.go b/audio/audio_config.go index 47f05ca..feffbf8 100644 --- a/audio/audio_config.go +++ b/audio/audio_config.go @@ -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) { diff --git a/dialog/callback_helpers.go b/dialog/callback_helpers.go index 3524b49..f04657b 100644 --- a/dialog/callback_helpers.go +++ b/dialog/callback_helpers.go @@ -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 +// #include +// #include import "C" var mu sync.Mutex @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/speech/callback_helpers.go b/speech/callback_helpers.go index 7e93689..0c8a4cb 100644 --- a/speech/callback_helpers.go +++ b/speech/callback_helpers.go @@ -8,6 +8,8 @@ import ( ) // #include +// #include +// #include import "C" var mu sync.Mutex @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/speech/session_event_args.go b/speech/session_event_args.go index 44884a3..f7a0dc6 100644 --- a/speech/session_event_args.go +++ b/speech/session_event_args.go @@ -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) @@ -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 } diff --git a/speech/speech_recognition_event_args.go b/speech/speech_recognition_event_args.go index 45f51d5..fb5cc97 100644 --- a/speech/speech_recognition_event_args.go +++ b/speech/speech_recognition_event_args.go @@ -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)