Skip to content

Commit

Permalink
Fix memory leak in RustBytes.
Browse files Browse the repository at this point in the history
I am very lucky to encounter this bug :).

BUG: 399796342
Change-Id: Ic6b6840e98808b486bf0c77150efde1afeaee017
  • Loading branch information
Changochen committed Feb 28, 2025
1 parent fccd13d commit a44e24f
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cc/ffi/rust_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class RustBytes {
: ffi_rust_bytes_(std::exchange(other.ffi_rust_bytes_, nullptr)) {}

RustBytes& operator=(RustBytes&& other) {
if (this == &other) return *this;
if (ffi_rust_bytes_ != nullptr) {
ffi::bindings::free_rust_bytes(ffi_rust_bytes_);
}
// Take ownership of the underlying pointer, and null out the source
// pointer so that we don't attempt to free it multiple times.
ffi_rust_bytes_ = other.ffi_rust_bytes_;
Expand Down

0 comments on commit a44e24f

Please sign in to comment.