Merge pull request #2011 from mozilla/free-strings

Provide an API function to free strings returned by API (Fixes #1979)
This commit is contained in:
Reuben Morais 2019-04-08 19:25:34 -03:00 committed by GitHub
commit 0b4b806cbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 8 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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.

View File

@ -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"

View File

@ -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;

View File

@ -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;