mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 14:16:16 -04:00
fixed a few missing deletes when handling errors in grouter. Experimenting a new scope guard to hold temporary memory. (Patch from GuessWho, modified)
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8093 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
50caf89655
commit
e58c9de067
4 changed files with 106 additions and 46 deletions
39
libretroshare/src/util/rsmemory.h
Normal file
39
libretroshare/src/util/rsmemory.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
// This is a scope guard to release the memory block when going of of the current scope.
|
||||
// Can be very useful to auto-delete some memory on quit without the need to call free each time.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// {
|
||||
// unsigned char *mem = NULL ;
|
||||
// TemporaryMemoryHolder mem_holder(mem,size) ;
|
||||
//
|
||||
// [do something]
|
||||
// } // mem gets freed automatically
|
||||
//
|
||||
class TemporaryMemoryHolder
|
||||
{
|
||||
public:
|
||||
TemporaryMemoryHolder(unsigned char *& mem,size_t s)
|
||||
: _mem(mem)
|
||||
{
|
||||
_mem = (unsigned char *)malloc(s) ;
|
||||
}
|
||||
|
||||
~TemporaryMemoryHolder()
|
||||
{
|
||||
if(_mem != NULL)
|
||||
{
|
||||
free(_mem) ;
|
||||
_mem = NULL ;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned char *& _mem ;
|
||||
};
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue