anonymous
anonymous

Reputation: 508

If I make objects inside a method in c++, will they get automatically destroyed when the function returns, or will it continue to eat memory?

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 ?

Upvotes: 1

Views: 524

Answers (3)

G B
G B

Reputation: 3024

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.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206727

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.

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 118011

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

Upvotes: 5

Related Questions