mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-16 15:01:15 +02:00
fixed potential memory leak in out-of-memory situations
This commit is contained in:
parent
34b9b1284c
commit
8935e19bbc
@ -73,6 +73,8 @@ private:
|
|||||||
MemoryPool(const MemoryPool&);
|
MemoryPool(const MemoryPool&);
|
||||||
MemoryPool& operator = (const MemoryPool&);
|
MemoryPool& operator = (const MemoryPool&);
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
BLOCK_RESERVE = 128
|
BLOCK_RESERVE = 128
|
||||||
|
@ -35,19 +35,35 @@ MemoryPool::MemoryPool(std::size_t blockLength, int preAlloc, int maxAlloc):
|
|||||||
if (maxAlloc > 0 && maxAlloc < r)
|
if (maxAlloc > 0 && maxAlloc < r)
|
||||||
r = maxAlloc;
|
r = maxAlloc;
|
||||||
_blocks.reserve(r);
|
_blocks.reserve(r);
|
||||||
for (int i = 0; i < preAlloc; ++i)
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
_blocks.push_back(new char[_blockSize]);
|
for (int i = 0; i < preAlloc; ++i)
|
||||||
|
{
|
||||||
|
_blocks.push_back(new char[_blockSize]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MemoryPool::~MemoryPool()
|
MemoryPool::~MemoryPool()
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MemoryPool::clear()
|
||||||
{
|
{
|
||||||
for (BlockVec::iterator it = _blocks.begin(); it != _blocks.end(); ++it)
|
for (BlockVec::iterator it = _blocks.begin(); it != _blocks.end(); ++it)
|
||||||
{
|
{
|
||||||
delete [] *it;
|
delete [] *it;
|
||||||
}
|
}
|
||||||
|
_blocks.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -77,7 +93,14 @@ void MemoryPool::release(void* ptr)
|
|||||||
{
|
{
|
||||||
FastMutex::ScopedLock lock(_mutex);
|
FastMutex::ScopedLock lock(_mutex);
|
||||||
|
|
||||||
_blocks.push_back(reinterpret_cast<char*>(ptr));
|
try
|
||||||
|
{
|
||||||
|
_blocks.push_back(reinterpret_cast<char*>(ptr));
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
delete [] reinterpret_cast<char*>(ptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user