diff --git a/native_client/client.cc b/native_client/client.cc index d2dc6085..04ba452e 100644 --- a/native_client/client.cc +++ b/native_client/client.cc @@ -246,7 +246,7 @@ ProcessFile(ModelState* context, const char* path, bool show_times) if (result.string) { printf("%s\n", result.string); - free((void*)result.string); + DS_FreeString((char*)result.string); } if (show_times) { diff --git a/native_client/deepspeech.cc b/native_client/deepspeech.cc index 344c301d..fc0b2004 100644 --- a/native_client/deepspeech.cc +++ b/native_client/deepspeech.cc @@ -926,6 +926,12 @@ DS_FreeMetadata(Metadata* m) } } +void +DS_FreeString(char* str) +{ + free(str); +} + void DS_PrintVersions() { std::cerr << "TensorFlow: " << tf_local_git_version() << std::endl; diff --git a/native_client/deepspeech.h b/native_client/deepspeech.h index 620d2558..7dba202a 100644 --- a/native_client/deepspeech.h +++ b/native_client/deepspeech.h @@ -116,8 +116,8 @@ int DS_EnableDecoderWithLM(ModelState* aCtx, * @param aBufferSize The number of samples in the audio signal. * @param aSampleRate The sample-rate of the audio signal. * - * @return The STT result. The user is responsible for freeing the string. - * Returns NULL on error. + * @return The STT result. The user is responsible for freeing the string using + * {@link DS_FreeString()}. Returns NULL on error. */ DEEPSPEECH_EXPORT char* DS_SpeechToText(ModelState* aCtx, @@ -187,7 +187,7 @@ void DS_FeedAudioContent(StreamingState* aSctx, * @param aSctx A streaming state pointer returned by {@link DS_SetupStream()}. * * @return The STT intermediate result. The user is responsible for freeing the - * string. + * string using {@link DS_FreeString()}. */ DEEPSPEECH_EXPORT char* DS_IntermediateDecode(StreamingState* aSctx); @@ -198,7 +198,8 @@ char* DS_IntermediateDecode(StreamingState* aSctx); * * @param aSctx A streaming state pointer returned by {@link DS_SetupStream()}. * - * @return The STT result. The user is responsible for freeing the string. + * @return The STT result. The user is responsible for freeing the string using + * {@link DS_FreeString()}. * * @note This method will free the state pointer (@p aSctx). */ @@ -235,7 +236,13 @@ void DS_DiscardStream(StreamingState* aSctx); * @brief Free memory allocated for metadata information. */ DEEPSPEECH_EXPORT -void DS_FreeMetadata(Metadata* m); +void DS_FreeMetadata(Metadata* m); + +/** + * @brief Free a char* string returned by the DeepSpeech API. + */ +DEEPSPEECH_EXPORT +void DS_FreeString(char* str); /** * @brief Print version of this library and of the linked TensorFlow library. diff --git a/native_client/java/jni/deepspeech.i b/native_client/java/jni/deepspeech.i index fb7caac1..50a65bf8 100644 --- a/native_client/java/jni/deepspeech.i +++ b/native_client/java/jni/deepspeech.i @@ -15,6 +15,11 @@ %pointer_functions(ModelState*, modelstatep); %pointer_functions(StreamingState*, streamingstatep); +%typemap(newfree) char* "DS_FreeString($1);"; +%newobject DS_SpeechToText; +%newobject DS_IntermediateDecode; +%newobject DS_FinishStream; + %rename ("%(strip:[DS_])s") ""; %include "../deepspeech.h" diff --git a/native_client/javascript/deepspeech.i b/native_client/javascript/deepspeech.i index 733465e4..a2a9e148 100644 --- a/native_client/javascript/deepspeech.i +++ b/native_client/javascript/deepspeech.i @@ -27,7 +27,7 @@ using namespace node; // make sure the string returned by SpeechToText is freed -%typemap(newfree) char* "free($1);"; +%typemap(newfree) char* "DS_FreeString($1);"; %newobject DS_SpeechToText; %newobject DS_IntermediateDecode; %newobject DS_FinishStream; diff --git a/native_client/python/impl.i b/native_client/python/impl.i index 03d08710..1617c029 100644 --- a/native_client/python/impl.i +++ b/native_client/python/impl.i @@ -33,7 +33,7 @@ import_array(); %append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0)); } -%typemap(newfree) char* "free($1);"; +%typemap(newfree) char* "DS_FreeString($1);"; %newobject DS_SpeechToText; %newobject DS_IntermediateDecode; %newobject DS_FinishStream;