Skip to content

Commit

Permalink
See memory of freed result to nullptr
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 570410751
  • Loading branch information
schmidt-sebastian authored and copybara-github committed Oct 3, 2023
1 parent d0183b2 commit a72839e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mediapipe/tasks/c/components/containers/category_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ void CppConvertToCategory(

void CppCloseCategory(Category* in) {
free(in->category_name);
in->category_name = nullptr;
free(in->display_name);
in->display_name = nullptr;
}

} // namespace mediapipe::tasks::c::components::containers
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,20 @@ void CppConvertToClassificationResult(

void CppCloseClassificationResult(ClassificationResult* in) {
for (uint32_t i = 0; i < in->classifications_count; ++i) {
auto classification_in = in->classifications[i];
auto& classification_in = in->classifications[i];

for (uint32_t j = 0; j < classification_in.categories_count; ++j) {
CppCloseCategory(&classification_in.categories[j]);
}
delete[] classification_in.categories;
classification_in.categories = nullptr;

free(classification_in.head_name);
classification_in.head_name = nullptr;
}

delete[] in->classifications;
in->classifications = nullptr;
}

} // namespace mediapipe::tasks::c::components::containers
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,20 @@ TEST(ClassificationResultConverterTest,
CppCloseClassificationResult(&c_classification_result);
}

TEST(ClassificationResultConverterTest, FreesMemory) {
mediapipe::tasks::components::containers::ClassificationResult
cpp_classification_result = {
/* classifications= */ {{{}, 0, "foo"}},
/* timestamp_ms= */ 42,
};

ClassificationResult c_classification_result;
CppConvertToClassificationResult(cpp_classification_result,
&c_classification_result);
EXPECT_NE(c_classification_result.classifications, nullptr);

CppCloseClassificationResult(&c_classification_result);
EXPECT_EQ(c_classification_result.classifications, nullptr);
}

} // namespace mediapipe::tasks::c::components::containers

0 comments on commit a72839e

Please sign in to comment.