fixing valgrind issue

This commit is contained in:
2025-06-03 11:57:58 +02:00
parent e72fe826b8
commit 8dfb6af3ec
3 changed files with 47 additions and 7 deletions

View File

@@ -37,7 +37,7 @@ namespace Scop
inline void SetOrientation(Quatf orientation) noexcept { m_orientation = orientation; } inline void SetOrientation(Quatf orientation) noexcept { m_orientation = orientation; }
inline void SetVisibility(bool show) noexcept { m_is_visible = show; } inline void SetVisibility(bool show) noexcept { m_is_visible = show; }
inline void SetIsOpaque(bool opaque) noexcept { m_is_opaque = opaque; } 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& GetPosition() const noexcept { return m_position; }
[[nodiscard]] inline const Vec3f& GetScale() const noexcept { return m_scale; } [[nodiscard]] inline const Vec3f& GetScale() const noexcept { return m_scale; }

View File

@@ -12,17 +12,57 @@ namespace Scop
{ {
public: public:
CPUBuffer() {} 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<std::uint8_t[]>(size) : nullptr), m_size(size)
{} {}
catch(...) catch(...)
{ {
FatalError("memory allocation for a CPU buffer failed"); FatalError("memory allocation for a CPU buffer failed");
} }
CPUBuffer(const CPUBuffer& other) try : m_data(other.m_size > 0 ? std::make_unique<std::uint8_t[]>(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<std::uint8_t[]> new_data;
try
{
new_data = std::make_unique<std::uint8_t[]>(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 [[nodiscard]] inline CPUBuffer Duplicate() const
{ {
CPUBuffer buffer(m_size); 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; return buffer;
} }
@@ -32,7 +72,7 @@ namespace Scop
FatalError("cannot allocate an already allocated CPU buffer"); FatalError("cannot allocate an already allocated CPU buffer");
try try
{ {
m_data = std::make_shared<std::uint8_t[]>(size); m_data = std::make_unique<std::uint8_t[]>(size);
m_size = size; m_size = size;
} }
catch(...) catch(...)
@@ -53,7 +93,7 @@ namespace Scop
~CPUBuffer() = default; ~CPUBuffer() = default;
private: private:
std::shared_ptr<std::uint8_t[]> m_data; std::unique_ptr<std::uint8_t[]> m_data;
std::size_t m_size = 0; std::size_t m_size = 0;
}; };
} }

View File

@@ -26,9 +26,9 @@ namespace Scop
file.read(reinterpret_cast<char*>(&bpp), sizeof(bpp)); file.read(reinterpret_cast<char*>(&bpp), sizeof(bpp));
file.seekg(54, std::ios_base::beg); file.seekg(54, std::ios_base::beg);
if(bpp != 24) 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 }; 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 }; Vec3b data{ 0, 0, 0 };
data.x = (file.eof() ? 0x00 : file.get()); data.x = (file.eof() ? 0x00 : file.get());