this stl code crashes, why???

Discussion in 'C++' started by Paul, Sep 2, 2004.

  1. Paul

    Paul Guest

    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.
     
    Paul, Sep 2, 2004
    #1
    1. Advertising

  2. Paul

    David Hilsee Guest

    "Paul" <> wrote in message
    news:...
    > 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;
    > }


    Well, I could see how this might cause infinite recursion. Your operator
    new calls std::map<void*,void*>::eek:perator[] which could call operator new,
    which calls std::map<void*,void*>::eek:perator[], which could call operator
    new, etc. That's just a guess, because I do not have VC++6. Have you
    considered using a debugger? It would probably give you a more
    straightforward answer than this newgroup.

    --
    David Hilsee
     
    David Hilsee, Sep 2, 2004
    #2
    1. Advertising

  3. * Paul:
    >
    > Global operator new and delete are overloaded and I am using stl map
    > to store pointers, but this code crashes


    That should read: and therefore this code crashes.

    Try to search for "infinite recursion".


    > int main(int argc, char* argv[])
    > {
    > return 0;
    > }


    This does not exercise the code and seems to serve no purpose.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Sep 2, 2004
    #3
  4. Paul

    PKH Guest

    "Paul" <> wrote in message
    news:...
    > 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
     
    PKH, Sep 2, 2004
    #4
  5. (Paul) wrote in message news:<>...

    > using namespace std;
    > typedef map<void*,void*> PointersMap;


    It will not solve the problem but

    typedef set< void * > PointersMap;

    would be more appropriate there IMHO. And there is erase method that
    accepts key instead of iterator in std::map so you do not have to do
    find/if/erase by yourself.
     
    Vyacheslav Kononenko, Sep 2, 2004
    #5
  6. Paul

    Paul Guest

    (Alf P. Steinbach) wrote in message news:<>...
    > * Paul:
    > >
    > > Global operator new and delete are overloaded and I am using stl map
    > > to store pointers, but this code crashes

    >
    > That should read: and therefore this code crashes.
    >


    Hmm, your point is valid, debugged but there is no infinite recursion.
    I am analyzing stack trace, 'll post the reason soon, it doesn't even
    call new again. I need to find out why.

    > Try to search for "infinite recursion".
    >
    >
    > > int main(int argc, char* argv[])
    > > {
    > > return 0;
    > > }

    >
    > This does not exercise the code and seems to serve no purpose.


    I wanted to show here that I am allocating nothing, still new is
    getting called and resulting in a crash. who is calling new and why?
    well I see that C Runtime is calling this, but why? Hmm looks like, I
    found some thing to learn.

    -Paul.
     
    Paul, Sep 2, 2004
    #6
  7. Paul wrote:
    >
    > (Alf P. Steinbach) wrote in message news:<>...
    > > * Paul:
    > > >
    > > > Global operator new and delete are overloaded and I am using stl map
    > > > to store pointers, but this code crashes

    > >
    > > That should read: and therefore this code crashes.
    > >

    >
    > Hmm, your point is valid, debugged but there is no infinite recursion.
    > I am analyzing stack trace, 'll post the reason soon, it doesn't even
    > call new again. I need to find out why.
    >
    > > Try to search for "infinite recursion".
    > >
    > >
    > > > int main(int argc, char* argv[])
    > > > {
    > > > return 0;
    > > > }

    > >
    > > This does not exercise the code and seems to serve no purpose.

    >
    > I wanted to show here that I am allocating nothing, still new is
    > getting called and resulting in a crash. who is calling new and why?


    Q: Well. What does your program do?
    A: It creates a PointersMap object.
    Q: So what is a PointersMap object?
    A: it is a std::map<void*,void*>
    Q: Could it be that when a std::map object
    comes into existance, that it allocates something?
    A: Hmm. Nobody knows for sure, but chances are high that
    it does.
    Q: But what does that mean?
    A: It means that the global operator new is called, which
    in turn uses the PointersMap object (which by the way is
    not constructed fully right now) and tries to insert a pointer
    in the map. As a consequence if this insertion, the PointersMap
    object will need to allocate some memory which results in a call
    to the global operator new, which in turn tries to insert the
    pointer, which results in a call to global operator new ....

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 2, 2004
    #7
  8. Paul

    Paul Guest

    "PKH" <> wrote in message news:<BOzZc.4608$g%>...
    > "Paul" <> wrote in message
    > news:...
    > > 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<void *,std::pair<void * const,void *>,std::map<void
    *,void *,std::less<void *>,std::allocator<void *>
    >::_Kfn,std::less<void *>,std::allocator<void *>
    >::_Parent(std::_Tree<void *,std::pair<void * const,void

    *>,std::map<void *,void *,std::less<void *>,std::allocator<void *>
    >::_Kfn,std::less<void *>,std::allocator<void *> >::_Node *

    0x00000000) line 42

    std::_Tree<void *,std::pair<void * const,void *>,std::map<void
    *,void *,std::less<void *>,std::allocator<void *>
    >::_Kfn,std::less<void *>,std::allocator<void *> >::_Root() line 550


    std::_Tree<void *,std::pair<void * const,void *>,std::map<void
    *,void *,std::less<void *>,std::allocator<void *>
    >::_Kfn,std::less<void *>,std::allocator<void *> >::insert(const

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

    std::map<void *,void *,std::less<void *>,std::allocator<void *>
    >::insert(const std::pair<void * const,void *> & {...}) line 96


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

    std::map<void *,void *,std::less<void *>,std::allocator<void *>
    >::eek: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::pair<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::pair<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::pair<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::pair<void * const,void *>,std::map<void *,void
    *,std::less<void *>,std::allocator<void *> >::_Kfn,std::less<void
    *>,std::allocator<void *> >::_Tree<void *,std::pair<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<void *,void *,std::less<void *>,std::allocator<void *>
    >::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
     
    Paul, Sep 3, 2004
    #8
  9. Paul

    David Hilsee Guest

    "Paul" <> wrote in message
    news:...
    > "PKH" <> wrote in message

    news:<BOzZc.4608$g%>...
    > > "Paul" <> wrote in message
    > > news:...
    > > > 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.


    I ran it on a copy of MSVC++6 I have at work, and you're right, it did not
    crash because of the recursion. However, there most certainly _is_
    recursion, and it would have infinitely recursed if it had been able to
    continue. It looked like it crashed because the std::map's constructor had
    not finished executing, so it was in an invalid state when operator[] was
    first called. The constructor would up invoking operator new, which invoked
    operator[] on the partially-constructed std::map.

    --
    David Hilsee
     
    David Hilsee, Sep 3, 2004
    #9
  10. * 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


    That is incorrect.


    > and hence no out-of stack message.


    The conclusion wouldn't follow from the premise if the premise was true.


    > 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.


    I and many others have already told you what the technical problem is.

    It is recursion.

    I've marked recursion below with "----------------- ^ -----------------".


    > *,void *,std::less<void *>,std::allocator<void *>
    > >::_Kfn,std::less<void *>,std::allocator<void *>
    > >::_Parent(std::_Tree<void *,std::pair<void * const,void

    > *>,std::map<void *,void *,std::less<void *>,std::allocator<void *>
    > >::_Kfn,std::less<void *>,std::allocator<void *> >::_Node *

    > 0x00000000) line 42
    >
    > std::_Tree<void *,std::pair<void * const,void *>,std::map<void
    > *,void *,std::less<void *>,std::allocator<void *>
    > >::_Kfn,std::less<void *>,std::allocator<void *> >::_Root() line 550

    >
    > std::_Tree<void *,std::pair<void * const,void *>,std::map<void
    > *,void *,std::less<void *>,std::allocator<void *>
    > >::_Kfn,std::less<void *>,std::allocator<void *> >::insert(const

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

    >
    > std::pair<void * const,void *>::pair<void * const,void *>(void *
    > const & 0x002f1000, void * const & 0x00000000) line 21
    >
    > std::map<void *,void *,std::less<void *>,std::allocator<void *>
    > >::eek: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::pair<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::pair<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::pair<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::pair<void * const,void *>,std::map<void *,void
    > *,std::less<void *>,std::allocator<void *> >::_Kfn,std::less<void
    > *>,std::allocator<void *> >::_Tree<void *,std::pair<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<void *,void *,std::less<void *>,std::allocator<void *>
    > >::map<void *,void *,std::less<void *>,std::allocator<void *> >(const

    > std::less<void *> & {...}, const std::allocator<void *> & {...}) line


    Called from here.


    > 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


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Sep 3, 2004
    #10
  11. Paul

    Paul Guest

    (Alf P. Steinbach) wrote in message news:<>...
    > * 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

    >
    > That is incorrect.
    >
    >
    > > and hence no out-of stack message.

    >
    > The conclusion wouldn't follow from the premise if the premise was true.
    >
    >
    > > 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.

    >
    > I and many others have already told you what the technical problem is.
    >
    > It is recursion.

    no its not, I agree this can cause recursion, but its not crashing due
    to recursion. David Hilsee's response addresses it properly.
    I modified the code to illustrate. this code'll not crash.
    like David said, the construction is not complete.
    It requests for memory - new allocates - but before returning the
    pointer to allocated memory to map - it uses that memory - which is
    why it crashes.
    #include <malloc.h>
    #include <map>

    using namespace std;
    typedef map<void*,void*> PointersMap;

    PointersMap gMemStore;
    bool bDone = false;

    void* operator new(size_t size)
    {
    void * p = ::malloc(size);
    if(bDone)
    gMemStore[p] = p;
    return p;
    }

    int main(int argc, char* argv[])
    {
    bDone = true;
    // following line when un-commented, results a crash due to recursion.
    // int *p = new int[10];
    return 0;
    }

    thanks for your comments.

    -Paul.
    >
    > I've marked recursion below with "----------------- ^ -----------------".
    >
    >
    > > *,void *,std::less<void *>,std::allocator<void *>
    > > >::_Kfn,std::less<void *>,std::allocator<void *>
    > > >::_Parent(std::_Tree<void *,std::pair<void * const,void

    > *>,std::map<void *,void *,std::less<void *>,std::allocator<void *>
    > > >::_Kfn,std::less<void *>,std::allocator<void *> >::_Node *

    > > 0x00000000) line 42
    > >
    > > std::_Tree<void *,std::pair<void * const,void *>,std::map<void
    > > *,void *,std::less<void *>,std::allocator<void *>
    > > >::_Kfn,std::less<void *>,std::allocator<void *> >::_Root() line 550

    > >
    > > std::_Tree<void *,std::pair<void * const,void *>,std::map<void
    > > *,void *,std::less<void *>,std::allocator<void *>
    > > >::_Kfn,std::less<void *>,std::allocator<void *> >::insert(const

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

    > >
    > > std::pair<void * const,void *>::pair<void * const,void *>(void *
    > > const & 0x002f1000, void * const & 0x00000000) line 21
    > >
    > > std::map<void *,void *,std::less<void *>,std::allocator<void *>
    > > >::eek: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::pair<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::pair<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::pair<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::pair<void * const,void *>,std::map<void *,void
    > > *,std::less<void *>,std::allocator<void *> >::_Kfn,std::less<void
    > > *>,std::allocator<void *> >::_Tree<void *,std::pair<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<void *,void *,std::less<void *>,std::allocator<void *>
    > > >::map<void *,void *,std::less<void *>,std::allocator<void *> >(const

    > > std::less<void *> & {...}, const std::allocator<void *> & {...}) line

    >
    > Called from here.
    >
    >
    > > 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
     
    Paul, Sep 3, 2004
    #11
  12. Paul

    Paul Guest

    (Alf P. Steinbach) wrote in message news:<>...
    > * 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

    >
    > That is incorrect.
    >
    >
    > > and hence no out-of stack message.

    >
    > The conclusion wouldn't follow from the premise if the premise was true.
    >
    >
    > > 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.

    >
    > I and many others have already told you what the technical problem is.
    >
    > It is recursion.
    >
    > I've marked recursion below with "----------------- ^ -----------------".
    >
    >
    > > *,void *,std::less<void *>,std::allocator<void *>
    > > >::_Kfn,std::less<void *>,std::allocator<void *>
    > > >::_Parent(std::_Tree<void *,std::pair<void * const,void

    > *>,std::map<void *,void *,std::less<void *>,std::allocator<void *>
    > > >::_Kfn,std::less<void *>,std::allocator<void *> >::_Node *

    > > 0x00000000) line 42
    > >
    > > std::_Tree<void *,std::pair<void * const,void *>,std::map<void
    > > *,void *,std::less<void *>,std::allocator<void *>
    > > >::_Kfn,std::less<void *>,std::allocator<void *> >::_Root() line 550

    > >
    > > std::_Tree<void *,std::pair<void * const,void *>,std::map<void
    > > *,void *,std::less<void *>,std::allocator<void *>
    > > >::_Kfn,std::less<void *>,std::allocator<void *> >::insert(const

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

    > >
    > > std::pair<void * const,void *>::pair<void * const,void *>(void *
    > > const & 0x002f1000, void * const & 0x00000000) line 21
    > >
    > > std::map<void *,void *,std::less<void *>,std::allocator<void *>
    > > >::eek: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::pair<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::pair<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::pair<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::pair<void * const,void *>,std::map<void *,void
    > > *,std::less<void *>,std::allocator<void *> >::_Kfn,std::less<void
    > > *>,std::allocator<void *> >::_Tree<void *,std::pair<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<void *,void *,std::less<void *>,std::allocator<void *>
    > > >::map<void *,void *,std::less<void *>,std::allocator<void *> >(const

    > > std::less<void *> & {...}, const std::allocator<void *> & {...}) line

    >
    > Called from here.
    >
    >
    > > 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


    you are correct on both counts, incomplete construction and possible
    recursion. Recursion is possible, but this crashes before even it
    reaches there, due to incomplete construction.

    thanks
    -Paul.
     
    Paul, Sep 3, 2004
    #12
  13. Paul

    Paul Guest

    "David Hilsee" <> wrote in message news:<>...
    > "Paul" <> wrote in message
    > news:...
    > > "PKH" <> wrote in message

    > news:<BOzZc.4608$g%>...
    > > > "Paul" <> wrote in message
    > > > news:...
    > > > > 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.

    >
    > I ran it on a copy of MSVC++6 I have at work, and you're right, it did not
    > crash because of the recursion. However, there most certainly _is_
    > recursion, and it would have infinitely recursed if it had been able to
    > continue. It looked like it crashed because the std::map's constructor had
    > not finished executing, so it was in an invalid state when operator[] was
    > first called. The constructor would up invoking operator new, which invoked
    > operator[] on the partially-constructed std::map.


    you are correct on both counts, incomplete construction and possible
    recursion. Recursion is possible, but this crashes before even it
    reaches there, due to incomplete construction.

    thanks
    -Paul.

    Pls. my second post under Alf P. Steinbach is a mistake, it should be here.
     
    Paul, Sep 4, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Ristroph
    Replies:
    6
    Views:
    3,020
    Rob Ristroph
    Jul 3, 2004
  2. Replies:
    2
    Views:
    1,016
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    914
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,013
    Smokey Grindel
    Dec 2, 2006
  5. Tmk
    Replies:
    2
    Views:
    427
Loading...

Share This Page