netlist/plib: Fix memory leak when exception is thrown in constructor.

This commit is contained in:
couriersud 2019-04-22 21:08:46 +02:00
parent 56f9e77b84
commit 7cc3012c22
2 changed files with 28 additions and 2 deletions

View File

@ -300,12 +300,29 @@ namespace plib {
#endif
}
#if 0
template<typename T, typename... Args>
owned_pool_ptr<T> make_poolptr(Args&&... args)
{
auto *mem = allocate(alignof(T), sizeof(T));
return owned_pool_ptr<T>(new (mem) T(std::forward<Args>(args)...), true, arena_deleter<aligned_arena, T>(*this));
}
#endif
template<typename T, typename... Args>
owned_pool_ptr<T> make_poolptr(Args&&... args)
{
auto *mem = allocate(alignof(T), sizeof(T));
try
{
auto *mema = new (mem) T(std::forward<Args>(args)...);
return owned_pool_ptr<T>(mema, true, arena_deleter<aligned_arena, T>(*this));
}
catch (std::exception &e)
{
deallocate(mem);
throw e;
}
}
};

View File

@ -168,7 +168,6 @@ namespace plib {
{
b->m_num_alloc--;
//printf("Freeing in block %p %lu\n", b, b->m_num_alloc);
sinfo().erase(it);
if (b->m_num_alloc == 0)
{
mempool *mp = b->m_mempool;
@ -179,6 +178,7 @@ namespace plib {
mp->m_blocks.erase(itb);
plib::pdelete(b);
}
sinfo().erase(it);
}
}
@ -189,7 +189,16 @@ namespace plib {
owned_pool_ptr<T> make_poolptr(Args&&... args)
{
auto *mem = this->allocate(alignof(T), sizeof(T));
return owned_pool_ptr<T>(new (mem) T(std::forward<Args>(args)...), true, arena_deleter<mempool, T>(this));
try
{
auto *mema = new (mem) T(std::forward<Args>(args)...);
return owned_pool_ptr<T>(mema, true, arena_deleter<mempool, T>(this));
}
catch (std::exception &e)
{
this->deallocate(mem);
throw e;
}
}
};