I have a pointer State *state;
which is a class that other states inherit from.
And I do state = new GameState;
Do I need to free the memory here somehow, if I change what the object state points to from GameState to something else?
Asked
Active
Viewed 72 times
-1

smog29
- 17
- 1
- 4
-
9Every `new` must be followed by a `delete`, otherwise you leak memory. – HolyBlackCat Nov 06 '20 at 19:06
-
3If you `new` it, it's your responsibility to `delete` it. Of course, if you only ever allocate one of those instances for the lifetime of your game, it's not much of a leak if you don't. – selbie Nov 06 '20 at 19:06
-
You won't need to free that unless you create too much object. Modern OS will clean up the whole memory used by application processes. – MikeCAT Nov 06 '20 at 19:06
-
1related/dupe: https://stackoverflow.com/questions/677812/is-there-a-reason-to-call-delete-in-c-when-a-program-is-exiting-anyway – NathanOliver Nov 06 '20 at 19:07
-
How can I delete the GameState object if I just used new GameState, without giving the object a name? – smog29 Nov 06 '20 at 19:07
-
That has to be collected some where, other there will be a memory leak. – csavvy Nov 06 '20 at 19:09
-
1According to your question you have a pointer to that object, so where is the problem? – UnholySheep Nov 06 '20 at 19:09
-
@smog29 By doing `delete state;`? If you didn't use `new`, you wouldn't need `delete` to begin with, so it's a weird question. – HolyBlackCat Nov 06 '20 at 19:09
-
If you are on C++11 or newer, you can use a smart pointer to not have to worry about memory management. `std::shared_ptr
state = std::make_shared – Lily Nov 06 '20 at 19:12();` or `std::shared_ptr state = std::make_shared ();` will create a pointer and automatically delete it when no longer being used by anything. -
@HolyBlackCat ye but I dont want to delete state, I want it to point to somewhere else and after it delete the GameState object, but how do I do it? – smog29 Nov 06 '20 at 19:15
-
`delete state;` doesn't delete the `state` itself. It deletes the thing it points to. After you do it, you can then write `state = ...;`. – HolyBlackCat Nov 06 '20 at 19:18
-
@smog29 When you use `new` (which you should almost never do), you need to make sure to keep a copy of the pointer somewhere so that you can later pass it to `delete` when you're done with the object. But modern C++ has much better tools for managing the lifetimes of dynamic objects. – David Schwartz Nov 06 '20 at 19:25
-
`new` and `delete` are an absolute
to use correctly. if `state` is the only pointer to an object dynamically allocated with `new`, you must `delete state;` before pointing `state` at a different object or the allocation will be leaked. The problem is *how do you know?* How do you know `state` points at an object dynamically allocated with `new`? How do you know it's the only pointer to the allocation and you're not ripping the allocation out from under some other piece of code? Pointers are real dumb, so unless you-the-programmer knows, no one does. – user4581301 Nov 06 '20 at 20:06
2 Answers
3
If what you mean is that State* state
does NOT own the object you will need to do something like this:
// create states
State* game_state = new GameState();
State* menu_state = new MenuState();
// set current state
State* state = game_state;
// ... do somethings ...
// delete states
delete game_state;
game_state = nullptr;
delete menu_state;
menu_state = nullptr;
state = nullptr;
If you want to write modern code use:
// create states
auto game_state = make_unique<GameState>();
auto menu_state = make_unique<MenuState>();
// set current state
State* state = game_state.get();
// ... do somethings ...
// no need to delete states
state = nullptr;
You could also use a combination of shared_ptr's and weak_ptr's.
If not using smart pointers, you need to call delete
on whatever owns the object.
If State* state
DOES own the object then you need to do:
// create current state
State* state = new GameState();
// ... do somethings ...
// switch state
delete state;
state = new MenuState();
// ... do somethings ...
// delete state
delete state;
state = nullptr;
And with smart pointers:
// create current state
unique_ptr<State> state = make_unique<GameState>();
// ... do somethings ...
// switch state (old state automatically deleted)
state = make_unique<MenuState>();
// ... do somethings ...
// delete state
state = nullptr;

Lily
- 1,386
- 2
- 13
- 16
1
Here is a simple rule: If you write new
, write a delete
for it. If you allocate memory on the heap, you have to free it, or you will get memory leaks, thats the nature of a language without built-in garbage collection.
So if you write:
State *state;
state = new GameState;
You also have to do:
delete state;
Also, consider using Smart pointers, they are safer and you should really look into them if you want to write modern C++.

Tom Gebel
- 744
- 1
- 4
- 13
-
You have to delete the object first, using the ```delete``` keyword, just as I made an example in my answer. Then you can re-assign the pointer. You can not delete an object you dont have the address from, thats whats causing the danger of memory leaks. Again, consider smart pointers. – Tom Gebel Nov 06 '20 at 19:30