fixed potential memory leak in out-of-memory situations

This commit is contained in:
Guenter Obiltschnig 2016-09-26 17:39:29 +02:00
parent 34b9b1284c
commit 8935e19bbc
2 changed files with 28 additions and 3 deletions

View File

@ -73,6 +73,8 @@ private:
MemoryPool(const MemoryPool&);
MemoryPool& operator = (const MemoryPool&);
void clear();
enum
{
BLOCK_RESERVE = 128

View File

@ -35,19 +35,35 @@ MemoryPool::MemoryPool(std::size_t blockLength, int preAlloc, int maxAlloc):
if (maxAlloc > 0 && maxAlloc < r)
r = maxAlloc;
_blocks.reserve(r);
try
{
for (int i = 0; i < preAlloc; ++i)
{
_blocks.push_back(new char[_blockSize]);
}
}
catch (...)
{
clear();
throw;
}
}
MemoryPool::~MemoryPool()
{
clear();
}
void MemoryPool::clear()
{
for (BlockVec::iterator it = _blocks.begin(); it != _blocks.end(); ++it)
{
delete [] *it;
}
_blocks.clear();
}
@ -77,7 +93,14 @@ void MemoryPool::release(void* ptr)
{
FastMutex::ScopedLock lock(_mutex);
try
{
_blocks.push_back(reinterpret_cast<char*>(ptr));
}
catch (...)
{
delete [] reinterpret_cast<char*>(ptr);
}
}