Skip to content

Commit 0f5fa0f

Browse files
wangtzxunkai55
authored andcommitted
NLClassifier C API: Fix memory leakage by releasing the copied c_str from strdup.
`strdup` is called in nl_classifier_c_api.cc:76 PiperOrigin-RevId: 374313022
1 parent de407f1 commit 0f5fa0f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

tensorflow_lite_support/cc/task/text/nlclassifier/nl_classifier_c_api_common.cc

+6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ limitations under the License.
1515

1616
#include "tensorflow_lite_support/cc/task/text/nlclassifier/nl_classifier_c_api_common.h"
1717

18+
#include <memory>
1819

1920
#ifdef __cplusplus
2021
extern "C" {
2122
#endif // __cplusplus
2223

2324
void NLClassifierCategoriesDelete(Categories* categories) {
25+
for (int i = 0; i < categories->size; i++) {
26+
// `strdup` obtains memory using `malloc` and the memory needs to be
27+
// released using `free`.
28+
free(categories->categories[i].text);
29+
}
2430
delete[] categories->categories;
2531
delete categories;
2632
}

tensorflow_lite_support/cc/task/text/qa/bert_qa_c_api.cc

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ void BertQuestionAnswererDelete(BertQuestionAnswerer* bert_question_answerer) {
7070
}
7171

7272
void BertQuestionAnswererQaAnswersDelete(QaAnswers* qa_answers) {
73+
for (int i = 0; i < qa_answers->size; i++) {
74+
// `strdup` obtains memory using `malloc` and the memory needs to be
75+
// released using `free`.
76+
free(qa_answers->answers[i].text);
77+
}
7378
delete[] qa_answers->answers;
7479
delete qa_answers;
7580
}

0 commit comments

Comments
 (0)