Commit cc25516d authored by Lukas Bergdoll's avatar Lukas Bergdoll Committed by Wouter van Oortmerssen

[C++] Fix DetachedBuffer move assignment lifetime (#4437)

* Fix DetachedBuffer move assignment lifetime

DetachedBuffer move assignment now destroys it's own content,
rather than waiting for the destructor of other.
See more under #4435

Add missing function return type

* Rename empty_assign to reset

Reset after destroy
reset() now operates on itself
parent 1d444f63
......@@ -445,32 +445,26 @@ class DetachedBuffer {
: allocator_(other.allocator_), own_allocator_(other.own_allocator_),
buf_(other.buf_), reserved_(other.reserved_), cur_(other.cur_),
size_(other.size_) {
other.allocator_ = nullptr;
other.own_allocator_ = false;
other.buf_ = nullptr;
other.reserved_ = 0;
other.cur_ = nullptr;
other.size_ = 0;
other.reset();
}
DetachedBuffer &operator=(DetachedBuffer &&other) {
std::swap(allocator_, other.allocator_);
std::swap(own_allocator_, other.own_allocator_);
std::swap(buf_, other.buf_);
std::swap(reserved_, other.reserved_);
std::swap(cur_, other.cur_);
std::swap(size_, other.size_);
destroy();
allocator_ = other.allocator_;
own_allocator_ = other.own_allocator_;
buf_ = other.buf_;
reserved_ = other.reserved_;
cur_ = other.cur_;
size_ = other.size_;
other.reset();
return *this;
}
~DetachedBuffer() {
if (buf_) {
assert(allocator_);
allocator_->deallocate(buf_, reserved_);
}
if (own_allocator_ && allocator_) {
delete allocator_;
}
destroy();
}
const uint8_t *data() const {
......@@ -515,6 +509,27 @@ class DetachedBuffer {
size_t reserved_;
uint8_t *cur_;
size_t size_;
inline void destroy() {
if (buf_) {
assert(allocator_);
allocator_->deallocate(buf_, reserved_);
}
if (own_allocator_ && allocator_) {
delete allocator_;
}
reset();
}
inline void reset() {
allocator_ = nullptr;
own_allocator_ = false;
buf_ = nullptr;
reserved_ = 0;
cur_ = nullptr;
size_ = 0;
}
};
// This is a minimal replication of std::vector<uint8_t> functionality,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment