PKH said:
Paul said:
Hi,
Global operator new and delete are overloaded and I am using stl map
to store pointers, but this code crashes, can some one shed some
light???
Compiler: MS VC++ 6.0
STL: Shipped with Visual Studio.
#include <malloc.h>
#include <map>
using namespace std;
typedef map<void*,void*> PointersMap;
PointersMap gMemStore;
void* operator new(size_t size)
{
void * p = ::malloc(size);
gMemStore[p] = p;
return p;
}
void operator delete(void* p)
{
PointersMap::iterator it = gMemStore.find(p);
if(it != gMemStore.end())
gMemStore.erase(it);
::free(p);
}
int main(int argc, char* argv[])
{
return 0;
}
TIA.
-Paul.
Do you crash with an out of stack-space message ?
The problem is probably as others have said that gMemStore[p] calls new,
giving an infinite loop.
You could create your own namespace to fix it, so you could use f.ex.
app::new and app::delete for you own code.
PKH
there is absolutely no recursion and hence no out-of stack message. It
crashes at first insert, I didn't see second insert or some sort of
recursion in stack trace.
interested people can have a look at this stack trace.
std::_Tree said:
::_Kfn,std::less<void *>,std::allocator<void *>
::_Parent(std::_Tree<void *,std:

air<void * const,void
::_Kfn,std::less<void *>,std::allocator<void *> >::_Node *
0x00000000) line 42
std::_Tree said:
::_Kfn,std::less<void *>,std::allocator<void *> >::_Root() line 550
std::_Tree said:
::_Kfn,std::less<void *>,std::allocator<void *> >::insert(const
std:

air<void * const,void *> & {...}) line 217 + 40 bytes
//****crash while it is unwinding****//
std::map said:
::insert(const std:

air<void * const,void *> & {...}) line 96
std:

air<void * const,void *>:

air<void * const,void *>(void *
const & 0x002f1000, void * const & 0x00000000) line 21
std::map said:
:

perator[](void * const & 0x002f1000) line 93
operator new(unsigned int 24) line 17 + 14 bytes
std::_Allocate(int 24, char * 0x00000000) line 30 + 9 bytes
std::allocator<void *>::_Charalloc(unsigned int 24) line 62 + 11 bytes
std::_Tree<void *,std:

air<void * const,void *>,std::map<void *,void
*,std::less<void *>,std::allocator<void *> >::_Kfn,std::less<void
*>,std::allocator<void *> >::_Buynode(std::_Tree<void *,std:

air<void
* const,void *>,std::map<void *,void *,std::less<void
*>,std::allocator<void *> >::_Kfn,std::less<void
*>,std::allocator<void *> >::_Node * 0x00000000, ...) line 578 + 10
bytes
std::_Tree<void *,std:

air<void * const,void *>,std::map<void *,void
*,std::less<void *>,std::allocator<void *> >::_Kfn,std::less<void
*>,std::allocator<void *> >::_Init() line 450 + 62 bytes
std::_Tree<void *,std:

air<void * const,void *>,std::map<void *,void
*,std::less<void *>,std::allocator<void *> >::_Kfn,std::less<void
*>,std::allocator<void *> >::_Tree<void *,std:

air<void * const,void
*>,std::map<void *,void *,std::less<void 1ee72c23(const std::less<void
*> & {...}, unsigned char 0, const std::allocator<void *> & {...})
line 160 + 67 bytes
std::map said:
::map<void *,void *,std::less<void *>,std::allocator<void *> >(const
std::less<void *> & {...}, const std::allocator<void *> & {...}) line
57 + 47 bytes
$E3() line 12 + 42 bytes
$E6() + 29 bytes
_initterm(void (void)* * 0x0042a104 $S7, void (void)* * 0x0042a208
___xc_z) line 525
_cinit() line 192 + 15 bytes
mainCRTStartup() line 205
KERNEL32! 7c581af6()
The representation...
-e (d called e)
-d (c called d)
-c (a called c)
-b (a called b, b returned and then a called c)
-a
tia.
-Paul