From 8dfb6af3ec0ab2e606b292de945a4ced4345a63e Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Tue, 3 Jun 2025 11:57:58 +0200 Subject: [PATCH] fixing valgrind issue --- ScopEngine/Runtime/Includes/Graphics/Actor.h | 2 +- ScopEngine/Runtime/Includes/Utils/Buffer.h | 48 +++++++++++++++++-- .../Runtime/Sources/Graphics/Loaders/BMP.cpp | 4 +- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/ScopEngine/Runtime/Includes/Graphics/Actor.h b/ScopEngine/Runtime/Includes/Graphics/Actor.h index cc49982..00e5005 100644 --- a/ScopEngine/Runtime/Includes/Graphics/Actor.h +++ b/ScopEngine/Runtime/Includes/Graphics/Actor.h @@ -37,7 +37,7 @@ namespace Scop inline void SetOrientation(Quatf orientation) noexcept { m_orientation = orientation; } inline void SetVisibility(bool show) noexcept { m_is_visible = show; } inline void SetIsOpaque(bool opaque) noexcept { m_is_opaque = opaque; } - inline void SetCustomPipeline(const CustomPipeline& pipeline) { m_custom_pipeline = pipeline; } + inline void SetCustomPipeline(CustomPipeline pipeline) { m_custom_pipeline = std::move(pipeline); } [[nodiscard]] inline const Vec3f& GetPosition() const noexcept { return m_position; } [[nodiscard]] inline const Vec3f& GetScale() const noexcept { return m_scale; } diff --git a/ScopEngine/Runtime/Includes/Utils/Buffer.h b/ScopEngine/Runtime/Includes/Utils/Buffer.h index bfc234c..e2a37c5 100644 --- a/ScopEngine/Runtime/Includes/Utils/Buffer.h +++ b/ScopEngine/Runtime/Includes/Utils/Buffer.h @@ -12,17 +12,57 @@ namespace Scop { public: CPUBuffer() {} - CPUBuffer(std::size_t size) try : m_data(new std::uint8_t[size]), m_size(size) + CPUBuffer(std::size_t size) try : m_data(size > 0 ? std::make_unique(size) : nullptr), m_size(size) {} catch(...) { FatalError("memory allocation for a CPU buffer failed"); } + CPUBuffer(const CPUBuffer& other) try : m_data(other.m_size > 0 ? std::make_unique(other.m_size) : nullptr), m_size(other.m_size) + { + if(m_data) + std::memcpy(m_data.get(), other.m_data.get(), m_size); + } + catch (...) + { + FatalError("memory allocation for a CPU buffer failed"); + } + + CPUBuffer& operator=(const CPUBuffer& other) + { + if(this != &other) + { + if(other.m_data) + { + std::unique_ptr new_data; + try + { + new_data = std::make_unique(other.m_size); + } + catch (...) + { + FatalError("memory allocation for a CPU buffer failed"); + } + std::memcpy(new_data.get(), other.m_data.get(), other.m_size); + + m_data = std::move(new_data); + m_size = other.m_size; + } + else + { + m_data.reset(); + m_size = 0; + } + } + return *this; + } + [[nodiscard]] inline CPUBuffer Duplicate() const { CPUBuffer buffer(m_size); - std::memcpy(buffer.GetData(), m_data.get(), m_size); + if(m_data) + std::memcpy(buffer.GetData(), m_data.get(), m_size); return buffer; } @@ -32,7 +72,7 @@ namespace Scop FatalError("cannot allocate an already allocated CPU buffer"); try { - m_data = std::make_shared(size); + m_data = std::make_unique(size); m_size = size; } catch(...) @@ -53,7 +93,7 @@ namespace Scop ~CPUBuffer() = default; private: - std::shared_ptr m_data; + std::unique_ptr m_data; std::size_t m_size = 0; }; } diff --git a/ScopEngine/Runtime/Sources/Graphics/Loaders/BMP.cpp b/ScopEngine/Runtime/Sources/Graphics/Loaders/BMP.cpp index 5f84966..086ca8b 100644 --- a/ScopEngine/Runtime/Sources/Graphics/Loaders/BMP.cpp +++ b/ScopEngine/Runtime/Sources/Graphics/Loaders/BMP.cpp @@ -26,9 +26,9 @@ namespace Scop file.read(reinterpret_cast(&bpp), sizeof(bpp)); file.seekg(54, std::ios_base::beg); if(bpp != 24) - Warning("BMP loader: warning while loadeing %, cannot handle yet different color palette sizes", path); + Warning("BMP loader: warning while loading %, cannot handle yet different color palette sizes", path); CPUBuffer buffer{ dimensions.x * dimensions.y * 4 }; - for(std::size_t i = 0; i <= buffer.GetSize(); i += 4) + for(std::size_t i = 0; i < buffer.GetSize(); i += 4) { Vec3b data{ 0, 0, 0 }; data.x = (file.eof() ? 0x00 : file.get());