Is there a memory leak here?

I'm not very comfortable with using new and delete, so I'm not quite sure if I'm doing this properly. iter is vector iterator of pointers to laser objects, and iter_ is a vector iterator of pointers to spaceship objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
std::vector<Laser*>::iterator iter = lasers.begin();
    std::vector<Spaceship*>::iterator iter_ = enemies.begin();

    while (iter != lasers.end())
    {
        // cut out for readability

        // Update enemy conditions (location/alive?)
        while (iter_ != enemies.end())
        {
            // cut out for readability

            // if the laser collides with a spaceship sprite...
            if ((*iter)->isColliding((*iter_)->sprite))
            {
                delete *iter_;  // delete the enemy
                iter_ = enemies.erase(iter_);

                delete *iter;
                iter = lasers.erase(iter);

            }
            else
                iter_++;
        }

        // Erase out-of-bounds lasers
        sf::Vector2f coords = (*iter)->sprite.GetPosition();
        if (coords.y >= 700 || coords.y <= -100)
        {
            delete *iter;
            iter = lasers.erase(iter);
        }
        else
            iter++;
    }


I'm pretty sure that the problem is coming from the possibility of the laser (iter, not iter_) being deleted twice.. but if I delete and remove the laser from the vector, shouldn't it be okay since it's being reset? (The code runs fine if I comment out deleting the laser upon a collision, with the exception that the laser can plow through all the enemies on the screen)
Why are you calling delete? I might be feeling a bit slow today and I might be wrong, but erasing all three lines with delete in them might fix the problem.

-Albatross
Last edited on
Oh, does vector.erase() take care of deleting new'd objects? The program still crashes, unless I comment out the first

1
2
delete *iter;                      //line 19
iter = lasers.erase(iter);


but otherwise I don't think I should have been calling delete then.
Oh, right. Silly me. Disregard what I said. std::vector::erase() does not delete pointers.

However, I'm curious. When does your program crash?

-Albatross
Last edited on
I'm having problems figuring it out, but I think it's when the laser collides with something. I do know that it is those two lines that are causing it to crash.
Yeah, I think the code attempts to delete the pointer twice.


At C:\Users\...\SFML\Game Attempts\Spacefighter\Game.cpp:52
Continuing...
At C:\Users\...\SFML\Game Attempts\Spacefighter\Game.cpp:55
Continuing...
Program received signal SIGSEGV, Segmentation fault


Those two lines are the two places where I have delete *iter.

I'm still confused as to what I should do, though..
Then I wonder. Is it possible for you to forcefully move a laser to an out-of-bounds range on the map (greater than 700 in the y direction)? EDIT: If not, then try allocating a boolean and setting it to true when the laser either collides with something or goes out of bounds, and then call delete for it once. EDIT2: Guess so. ;)

-Albatross
Last edited on
Oh, thanks, that works! Much simpler and easier too :)
new and delete are sibling, looks like your pointer didn't use any new keyword to allocate memory, so why are you using delete?
new is used elsewhere in the code.
Topic archived. No new replies allowed.