Assuming that your Game
owns the objects, then your Game
destructor would need to free up the memory. Presumably, the Character
s are allocated with new
. The below will delete all the Character
s in the board
, then the class variables (like the vector
) will automatically be freed afterward.
~Game() {
for ( Character *character : board )
delete character;
}
Unless this is for an exercise with pointers, it's generally recommended not to use bare-pointers, and instead use a smart pointer, such as unique_ptr
or shared_ptr
.
std::vector<std::shared_ptr<Character>> board;
In this case, there will be no leak, since the Character
s will automatically be freed once no one points to them anymore.
Use of shared_ptr
vs unique_ptr
is dependent on what you're doing.