Skip to content

Commit 69fab28

Browse files
author
devsh
committed
fix a silly memory leak in the IShaderCompiler::CCache
1 parent 9412472 commit 69fab28

File tree

3 files changed

+26
-66
lines changed

3 files changed

+26
-66
lines changed

include/nbl/core/alloc/VectorViewNullMemoryResource.h

Lines changed: 0 additions & 56 deletions
This file was deleted.

include/nbl/core/alloc/refctd_memory_resource.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
namespace nbl::core
1414
{
1515

16-
class refctd_memory_resource : public IReferenceCounted
16+
class NBL_API2 refctd_memory_resource : public IReferenceCounted
1717
{
1818
public:
1919
virtual void* allocate(size_t bytes, size_t alignment) = 0;
2020

2121
virtual void deallocate(void* p, size_t bytes, size_t alignment) = 0;
2222
};
23+
NBL_API2 smart_refctd_ptr<refctd_memory_resource> getDefaultMemoryResource();
24+
NBL_API2 void setDefaultMemoryResource(refctd_memory_resource* memoryResource);
2325

2426
class std_memory_resource final : public refctd_memory_resource
2527
{
@@ -39,10 +41,26 @@ class std_memory_resource final : public refctd_memory_resource
3941
private:
4042
std::pmr::memory_resource* m_pmr;
4143
};
42-
4344
NBL_API2 smart_refctd_ptr<std_memory_resource> getNullMemoryResource();
44-
NBL_API2 smart_refctd_ptr<refctd_memory_resource> getDefaultMemoryResource();
45-
NBL_API2 void setDefaultMemoryResource(refctd_memory_resource* memoryResource);
45+
46+
template<typename T>
47+
class adoption_memory_resource final : public refctd_memory_resource
48+
{
49+
public:
50+
inline adoption_memory_resource(T&& _backer) : m_backer(std::move(_backer)) {}
51+
52+
inline void* allocate(std::size_t bytes, std::size_t alignment) override
53+
{
54+
assert(false); // should never be called
55+
}
56+
inline void deallocate(void* p, std::size_t bytes, std::size_t alignment) override {} // noop
57+
58+
T& getBacker() {return m_backer;}
59+
const T& getBacker() const {return m_backer;}
60+
61+
protected:
62+
T m_backer;
63+
};
4664

4765
}
4866

src/nbl/asset/utils/IShaderCompiler.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "nbl/asset/utils/shadercUtils.h"
66
#include "nbl/asset/utils/shaderCompiler_serialization.h"
77

8-
#include "nbl/core/alloc/VectorViewNullMemoryResource.h"
9-
108
#include <sstream>
119
#include <regex>
1210
#include <iterator>
@@ -343,8 +341,8 @@ core::smart_refctd_ptr<ICPUBuffer> IShaderCompiler::CCache::serialize() const
343341
// Might as well memcpy everything
344342
memcpy(retVal.data() + SHADER_BUFFER_SIZE_BYTES + shaderBufferSize, dumpedContainerJson.data(), dumpedContainerJsonLength);
345343

346-
auto memoryResource = new core::VectorViewNullMemoryResource(std::move(retVal));
347-
return ICPUBuffer::create({ { retValSize }, memoryResource->data(), core::make_smart_refctd_ptr<core::refctd_memory_resource>(memoryResource) });
344+
auto memoryResource = core::make_smart_refctd_ptr<core::adoption_memory_resource<decltype(retVal)>>(std::move(retVal));
345+
return ICPUBuffer::create({ { retValSize }, memoryResource->getBacker().data(),std::move(memoryResource)});
348346
}
349347

350348
core::smart_refctd_ptr<IShaderCompiler::CCache> IShaderCompiler::CCache::deserialize(const std::span<const uint8_t> serializedCache)
@@ -417,8 +415,8 @@ bool nbl::asset::IShaderCompiler::CCache::SEntry::setContent(const asset::ICPUBu
417415
if (res != SZ_OK || propsSize != LZMA_PROPS_SIZE) return false;
418416
compressedSpirv.resize(propsSize + destLen);
419417

420-
auto memResource = new core::VectorViewNullMemoryResource(std::move(compressedSpirv));
421-
spirv = ICPUBuffer::create({ { propsSize + destLen }, memResource->data(), core::make_smart_refctd_ptr<core::refctd_memory_resource>(memResource) });
418+
auto memoryResource = core::make_smart_refctd_ptr<core::adoption_memory_resource<decltype(compressedSpirv)>>(std::move(compressedSpirv));
419+
spirv = ICPUBuffer::create({ { propsSize + destLen }, memoryResource->getBacker().data(),std::move(memoryResource)});
422420

423421
return true;
424422
}

0 commit comments

Comments
 (0)