Skip to content

Commit 85351ec

Browse files
frankosterfeldwirew0rm
authored andcommitted
Do not allocate in IoBuffer(IoBuffer&&)
resize() allocated memory in the size of the moved-from buffer, which was then leaked, do not do that. Signed-off-by: Frank Osterfeld <[email protected]>
1 parent 25caeba commit 85351ec

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/serialiser/include/IoBuffer.hpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,11 @@ struct IoBuffer {
262262
}
263263

264264
[[nodiscard]] IoBuffer(IoBuffer &&other) noexcept
265-
: _allocator(other._allocator.select_on_container_copy_construction()) {
266-
_buffer = resize(other._size);
267-
_position = other._position;
268-
_capacity = other.capacity();
269-
_buffer = other._buffer;
270-
other._buffer = nullptr;
271-
}
265+
: _allocator(other._allocator.select_on_container_copy_construction())
266+
, _buffer(std::exchange(other._buffer, nullptr))
267+
, _position(other._position)
268+
, _size(other._size)
269+
, _capacity(other._capacity) {}
272270

273271
constexpr ~IoBuffer() {
274272
freeInternalBuffer();
@@ -288,6 +286,9 @@ struct IoBuffer {
288286
}
289287

290288
IoBuffer &operator=(IoBuffer &&other) noexcept {
289+
if (this == &other) {
290+
return *this;
291+
}
291292
auto temp = std::move(other);
292293
swap(temp);
293294
return *this;

0 commit comments

Comments
 (0)