Skip to content

Commit

Permalink
ffi: leak: remember to call destructor in dart_deleter
Browse files Browse the repository at this point in the history
Otherwise `unique_dart_ptr<DecodeBarcodeParams>` accidentally leaks the
inner image bytes!
  • Loading branch information
phlip9 committed Jun 12, 2024
1 parent f6f4c3f commit 7d2b7aa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/dart_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ struct dart_deleter {
template <typename T>
void operator()(T* p) const noexcept
{
dart_allocator<T>{}.deallocate(p, 1);
if (p != nullptr) {
// A "deleter" also needs to run p's destructor before free'ing.
p->~T();
dart_allocator<T>{}.deallocate(p, 1);
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/native_zxing.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern "C"
dart_free(bytes);
}

// Disable copy/move contsructors
DecodeBarcodeParams(const DecodeBarcodeParams&) = delete;
DecodeBarcodeParams& operator=(const DecodeBarcodeParams&) = delete;
DecodeBarcodeParams(DecodeBarcodeParams&&) = delete;
Expand All @@ -71,6 +72,7 @@ extern "C"
dart_free(contents);
}

// Disable copy/move contsructors
EncodeBarcodeParams(const EncodeBarcodeParams&) = delete;
EncodeBarcodeParams& operator=(const EncodeBarcodeParams&) = delete;
EncodeBarcodeParams(EncodeBarcodeParams&&) = delete;
Expand Down

0 comments on commit 7d2b7aa

Please sign in to comment.