J
Joerg Toellner
Hi Group,
i stumbled over a delete problem which confuses me. I'm not very experienced
with C++ programming and so i am not able to understand what's going on
here.
I have the following situation:
-------------SNIP---------------------
// File: map.cpp
class tile;
class map
{
map();
~map();
// ... some other unrelated stuff here
tile *m_MyTiles[144];
}
void map::map()
{
int x;
for(x = 0; x < 144; x++)
m_MyTiles[x] = new tile;
}
void map::~map()
{
for(x = 0; x < 144; x++)
delete m_MyTiles[x];
}
-------------SNIP---------------------
When i construct a new instance of class map and later delete it again my
program crash at the delete command in the destructor. It seems that the (in
the constructor of class map) created 144 instances of class Tile will be
deleted somehow automagically. When i comment out the code in the destructor
of class map, all works fine. But i assume that then i will leave some
memory leaks behind, right?
Can s.o. please direct me in the right direction what i'm doing wrong? Must
i delete my with new created instances of the tiles in the destructor of map
(where the pointers are member variables)? Or who will do this for me behind
the scene? Or when this will leave memory leaks, how do i delete my
tiles-objects correctly without a program crash?
Sorry for the dumb question, but i can't figure it out myself.
TIA very much
Joerg Toellner
i stumbled over a delete problem which confuses me. I'm not very experienced
with C++ programming and so i am not able to understand what's going on
here.
I have the following situation:
-------------SNIP---------------------
// File: map.cpp
class tile;
class map
{
map();
~map();
// ... some other unrelated stuff here
tile *m_MyTiles[144];
}
void map::map()
{
int x;
for(x = 0; x < 144; x++)
m_MyTiles[x] = new tile;
}
void map::~map()
{
for(x = 0; x < 144; x++)
delete m_MyTiles[x];
}
-------------SNIP---------------------
When i construct a new instance of class map and later delete it again my
program crash at the delete command in the destructor. It seems that the (in
the constructor of class map) created 144 instances of class Tile will be
deleted somehow automagically. When i comment out the code in the destructor
of class map, all works fine. But i assume that then i will leave some
memory leaks behind, right?
Can s.o. please direct me in the right direction what i'm doing wrong? Must
i delete my with new created instances of the tiles in the destructor of map
(where the pointers are member variables)? Or who will do this for me behind
the scene? Or when this will leave memory leaks, how do i delete my
tiles-objects correctly without a program crash?
Sorry for the dumb question, but i can't figure it out myself.
TIA very much
Joerg Toellner