Thread: Delete causing SIGSEGV

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Delete causing SIGSEGV

    Code:
    for(std::vector<Entity*>::iterator TempIterator = mList.begin(); TempIterator != mList.end();)
        {
            (*TempIterator)->Update();
            if((*TempIterator)->IsDead())
            {
                delete (*TempIterator);
                TempIterator = mList.erase(TempIterator);
            }
            else TempIterator++;
        }
    This is the culprit code, all the Entities in mList is created with new, but for some reason it crashes at the Update() function in the above code. This doesnt happen if i comment out the delete and just erase the pointer from the array, but this obviously means memory-leaking. Entity is the base class for a bunch of other classes that reaches this piece of code, not sure if that means anything?
    I'm stuck, can you guys spot anything suspicious in the code above? If not then atleast i will know the problem is somewhere else.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Any possible number of explanations. All involve some form of undefined behaviour.

    If some pointer is somehow being stored in two distinct vectors, the code above will delete an object twice (upon being invoked for both vectors).

    One or more of the vectors in the array is NULL, or otherwise not a valid object. The calls to Update() or IsDead() then have unwanted effects.

    One of more of the pointers in the vector were returned by the array operator new (i.e. new Entity[some_number]).

    If some other code, executed before the code above is invoked, molests a pointer, it can overwrite some random area of memory. If that area of memory is part of data structures managed by operators new and delete, the functioning of operator delete can be compromised.

    One of the often forgotten rules of undefined behaviour is that the cause of a crash is some code executed at or before the point where the crash occurs. A program crash just means the error is detected, usually by the operating system, which then terminates the program. The underlying cause, however, can remain undetected for some time, and symptoms do not occur until it is detected.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Thanks for trying to help Grumpy!
    I took a lengthy break from staring at the code, and when i came back it hit me.

    I was adding rockets to a vector in my spaceship class whenever space is pushed, i was then passing this list to my entity manager which amongst other things runs the code i posted above. The entity manager added the elements to the mList vector which holds all entities, however i did not clear the vector in the spaceship each time i passed it on to the manager class, so each rocket had multiple pointers pointing to it in the mList vector. This meant that whenever a rocket returned true for IsDead(), it would be deleted, and alot of subsequent pointers in mList would be pointing nowhere. Calling Update() on a random memory address obviously results in mayhem.

    This also fixed some weird issue i was having with the rockets moving way to fast, obvious since Update() would be called on each rocket multiple times each frame.

    Oh well, rookie mistake i guess!
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Causing and trapping SIGSEGV
    By ampersand11 in forum C Programming
    Replies: 2
    Last Post: 01-22-2008, 05:26 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM