attempt at fixing the crash when quitting RS (due to operator new not being able to allocate memory from deleted allocator

This commit is contained in:
csoler 2016-06-06 22:55:23 -04:00
parent 54a0ca4678
commit 1c1b7acef6

View File

@ -279,11 +279,22 @@ void *SmallObject::operator new(size_t size)
#endif #endif
RsStackMutex m(_mtx) ; RsStackMutex m(_mtx) ;
void *p = _allocator.allocate(size) ;
// This should normally not happen. But that prevents a crash when quitting, since we cannot prevent the constructor
// of an object to call operator new(), nor to handle the case where it returns NULL.
// The memory will therefore not be deleted if that happens. We thus print a warning.
if(_allocator._active)
return _allocator.allocate(size) ;
else
{
std::cerr << "(EE) allocating " << size << " bytes of memory that cannot be deleted. This is a bug, except if it happens when closing Retroshare" << std::endl;
return malloc(size) ;
}
#ifdef DEBUG_MEMORY #ifdef DEBUG_MEMORY
std::cerr << "new RsItem: " << p << ", size=" << size << std::endl; std::cerr << "new RsItem: " << p << ", size=" << size << std::endl;
#endif #endif
return p ;
} }
void SmallObject::operator delete(void *p,size_t size) void SmallObject::operator delete(void *p,size_t size)