1
void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const
{

    sf::CircleShape c0;
    c0.setFillColor(sf::Color::Red);
    c0.setPointCount(4);
    c0.setRadius(1);
    c0.setPosition(sf::Vector2f(point[0].x, point[0].y));

    sf::CircleShape c1;
    c1.setFillColor(sf::Color::Red);
    c1.setPointCount(4);
    c1.setRadius(1);
    c1.setPosition(sf::Vector2f(point[1].x, point[1].y));

    target.draw(c0);
    target.draw(c1);
}

I am learning c++. As you can see I am making the CircleShape objects inside the draw method which runs at 60 times in a second. I read online that objects in c++ are stored in heap memory therefore require manual deallocation. Do the objects c0 and c1 get destroyed and the memory frees when draw method returns, or will they continue to eat heap memory ?

anonymous
  • 448
  • 1
  • 6
  • 25

3 Answers3

5

When you declare variables on the stack, their destructors will get called when they fall out of scope

{
    int a = 5;
}
// a is now out of scope, it's destructor was called

You would have to go out of your way to clean up the memory if you allocated from the heap

{
    int* a = new int(5);
}
// a was just leaked, unless you had a pointer to it later

You would have to have cleaned it up like

{
    int* a = new int(5);
    delete a;
}

As of C++11, I would strongly advocate for using <memory> if you need heap allocated memory, but still want RAII cleanup.

#include <memory>
{
    std::unique_ptr<int> a = std::make_unique<int>(5);
}
// a falls out of scope, the unique_ptr ensures the memory is cleaned up
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    He would have to clean it up like `unique_ptr`, not like `delete`. – Puppy May 11 '15 at 14:43
  • 5
    @Puppy He **could** but he does not **have to**. Using `` allows for RAII managed heap memory, which is great and I advocate for people to go that route, I was trying to be clear on when you do and do not have to clean up memory manually. – Cory Kramer May 11 '15 at 14:43
  • @Puppy - `unique_ptr` seems better if there might be exceptions thrown somewhere inside function, but in normal function it might be as well as plain-old delete, why all the fuss? – RippeR May 11 '15 at 14:44
  • He pretty much does have to if he wants it to be correct and be able to keep it correct and use the same pattern elsewhere and have it still be correct. – Puppy May 11 '15 at 14:44
  • 2
    @Puppy You keep saying "have to" when you mean "should". "Have to" implies ill-formed, illegal, or undefined code. "Should" implies a strong preference. – Cory Kramer May 11 '15 at 14:46
  • I said exactly what I meant. – Puppy May 11 '15 at 14:46
  • Indeed I am. Nobody ever maintains a function and changes it so that it now throws when it did not before- especially beginners. And nobody ever takes a sample and applies the same pattern all across their codebase- especially beginners. And nobody ever did an early return, or stopped needing a resource in one branch, or anything like that. – Puppy May 11 '15 at 14:48
1

The code you wrote only creates temporary objects on the stack, which are destroyed when the method exits.

In general, when your variables go out of scope, their content is no longer available.

If you allocate memory using the "new" keyword, then your objects will be kept in memory, but the pointer is lost when the variable referencing them goes out of scope.

Edit:

Actually, "temporary objects" are a feature of the C++ language, which can cause headaches if not used correctly. Read more here: Temporary objects - when are they created, how do you recognise them in code?

The objects you create as local variables inside a function are automatically allocated and destroyed, I only used the word "temporary" to describe their behaviour: they are discarded when the function exits.

Community
  • 1
  • 1
G B
  • 2,951
  • 2
  • 28
  • 50
0

Do the objects c0 and c1 get destroyed and the memory frees when draw method returns,

Yes. They get destroyed when the function returns.

or will they continue to eat heap memory ?

The answer to that depends on what you do in the constructor and the destructor of the class. If you allocate memory from heap in the constructor or any of the other member function, that memory should be freed in the destructor. Otherwise, your function will leak memory, indirectly.

R Sahu
  • 204,454
  • 14
  • 159
  • 270