From 8597b4acb2fe531ef5086f502bf1a261d2424a86 Mon Sep 17 00:00:00 2001 From: reda ghouzraf <98324229+rghouzra@users.noreply.github.com> Date: Sat, 30 Nov 2024 01:17:22 +0100 Subject: [PATCH] Fixes move behaviour of chunk container (#152) --- src/XAD/ChunkContainer.hpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/XAD/ChunkContainer.hpp b/src/XAD/ChunkContainer.hpp index ee6d2ea..ae89a69 100644 --- a/src/XAD/ChunkContainer.hpp +++ b/src/XAD/ChunkContainer.hpp @@ -133,16 +133,20 @@ class ChunkContainer } ChunkContainer(ChunkContainer&& o) noexcept - : chunkList_(std::move(o.chunkList_)), chunk_(o.chunk_), idx_(o.idx_) - { + : chunkList_(std::move(o.chunkList_)), chunk_(o.chunk_), idx_(o.idx_) { + o.chunk_ = 0; + o.idx_ = 0; } - ChunkContainer& operator=(ChunkContainer&& o) - { - _free_memory(); - chunkList_ = std::move(o.chunkList_); - chunk_ = o.chunk_; - idx_ = o.idx_; + ChunkContainer& operator=(ChunkContainer&& o) noexcept { + if (this != &o) { + _free_memory(); + chunkList_ = std::move(o.chunkList_); + chunk_ = o.chunk_; + idx_ = o.idx_; + o.chunk_ = 0; + o.idx_ = 0; + } return *this; } @@ -445,8 +449,9 @@ class ChunkContainer { char* chunk = reinterpret_cast( detail::aligned_alloc(ALIGNMENT, sizeof(value_type) * chunk_size)); - if (chunk == NULL) + if (chunk == nullptr) { throw std::bad_alloc(); + } chunkList_.push_back(chunk); } ++chunk_;