agmcleod
agmcleod

Reputation: 13621

Object cleanup with pointers

Just want to know if the following is true, as I work towards a reset feature in a small game I'm working on.

So if I have a setup like so:

class Game {
public:
  Game(Ball b, Paddle one, Paddle two) : b(b), one(one), two(two) { }
  void initGame();
  void resetGame();
private:
  Ball b;
  Paddle one;
  Paddle two;
  std::vector<GameObject *> objects;
};

Game::initGame() {
  objects.push_back(&b);
  objects.push_back(&one);
  objects.push_back(&two);
}

Game::resetGame() {
  while (!objects.empty()) {
    objects.pop_back();
  }
  b = Ball();
  one = Paddle();
  two = Paddle();

  initGame();
}

My question is with the resetGame method. I empty out the array of pointers, and then replace the objects below. Now, given they were pointers to pieces of memory, and the original objects get replaced, does the original memory leak? Should i have deleted the data first? I know delete is used in conjunction with new, but I'm not sure if the compiler cleans this up for me.

Thanks.

Upvotes: 1

Views: 71

Answers (1)

Steve
Steve

Reputation: 1810

No, no leaks here because the memory is not dynamically allocated.

Upvotes: 3

Related Questions