This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Problem with new/delete appeared with gcc-3.4.x
- From: mws <mws at twisted-brains dot org>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 25 Oct 2004 16:03:00 +0200
- Subject: Problem with new/delete appeared with gcc-3.4.x
Hi,
i have got a memory tracking class in one of my projects.
please find somewhere down this mail my header an implementation. with gcc-3.3.x everything is working properly and successfull.
when compiled with gcc-3.4.x i get a deadlock within the removeTrack(...) function. in my opinion this is caused by std::list using my overloaded delete operator;
how can this be solved - any solutions?
regards
Marcel Siegert
mws@twisted-brains.org
the header file is included in some of my .cpp implementation files.
header ::
#ifndef memdebug
#define memdebug
#include "utils/mutex.h"
#include <list>
class CMemoryTrack
{
private:
struct SMemAllocInfo
{
unsigned int address;
unsigned int size;
char file[64];
char function[200];
unsigned int line;
unsigned int type;
};
typedef std::list<SMemAllocInfo*> CMemAllocList;
static CMemAllocList memAllocList;
static CMutex memLock;
public:
static void addTrack(unsigned int addr, unsigned int asize, const char *fname, const char *functionName, unsigned int lnum, unsigned int type);
static void removeTrack(unsigned int addr, unsigned int type);
static void lookupTrack(unsigned int addr);
static void dumpUnfreed();
};
inline void * operator new(unsigned int size, const char *file, const char *function, int line)
{
void *ptr = (void *)malloc(size);
CMemoryTrack::addTrack((unsigned int)ptr, size, file, function, line, 1);
return(ptr);
};
inline void operator delete(void *p)
{
CMemoryTrack::removeTrack((unsigned int)p,1);
free(p);
};
inline void * operator new[](unsigned int size, const char *file, const char *function, int line)
{
void *ptr = (void *)malloc(size);
CMemoryTrack::addTrack((unsigned int)ptr, size, file, function, line, 2);
return(ptr);
};
inline void operator delete[](void *p)
{
CMemoryTrack::removeTrack((unsigned int)p, 2);
free(p);
};
#define new new(__FILE__, __PRETTY_FUNCTION__, __LINE__)
#endif // memdebug
- now the implemenation which is part of a libdebug which is linked to the other implementations after compile
// at first undef new to prevent a deadlock in addTrack function
#undef new
// initialize static members
// memLock is a implementation of a normal mutex - just done to get rid of pthread_mutex_lock(&mutex) != 0 statement overall in my project files.
CMemoryTrack::CMemAllocList CMemoryTrack::memAllocList;
CMutex CMemoryTrack::memLock;
void CMemoryTrack::addTrack(unsigned int addr, unsigned int asize, const char *fname, const char *functionName, unsigned int lnum, unsigned int type)
{
SMemAllocInfo *info;
info = new(SMemAllocInfo);
info->address = addr;
strncpy(info->file, fname, 63);
strncpy(info->function, functionName, 199);
info->line = lnum;
info->size = asize;
info->type = type;
memLock.lock();
memAllocList.insert(memAllocList.begin(), info);
memLock.unLock();
}
void CMemoryTrack::removeTrack(unsigned int addr, unsigned int type)
{
memLock.lock();
CMemAllocList::iterator i;
for(i = memAllocList.begin(); i != memAllocList.end(); ++i)
{
if ( (*i)->type == 3 )
{
continue;
}
if((*i)->address == addr)
{
if ( (*i)->type != type )
{
(*i)->type=3;
break;
}
memAllocList.erase(i);
break;
}
}
memLock.unLock();
}
void CMemoryTrack::lookupTrack(unsigned int addr)
{
memLock.lock();
CMemAllocList::iterator i;
bool found = false;
for(i = memAllocList.begin(); i != memAllocList.end(); ++i)
{
if ( (*i)->type == 3 )
continue;
if((*i)->address == addr)
{
printf("lookup address 0x%0x has been alloced by %25s %5d\n", addr, (*i)->file, (*i)->line);
found = true;
break;
}
}
if (!found)
printf("lookup address 0x%0x could not be resolved\n", addr);
memLock.unLock();
}
// end of implemenation