Re: confusing delete problem
From: Joerg Toellner (toellner_at_oss-gmbh.de)
Date: 02/24/04
- Next message: John Harrison: "Re: confusing delete problem"
- Previous message: John Harrison: "Re: read problem"
- In reply to: John Harrison: "Re: confusing delete problem"
- Next in thread: John Harrison: "Re: confusing delete problem"
- Reply: John Harrison: "Re: confusing delete problem"
- Reply: Karl Heinz Buchegger: "Re: confusing delete problem"
- Reply: Thomas Matthews: "Re: confusing delete problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 24 Feb 2004 16:41:11 +0100
Hi John and Clark,
thx for your replies.
I haven't my code right by hand now. So i only wrote the situation out of
the head. Of course x is defined in the real code destructor.
I'm glad that my suggestion that i have to delete it, is right.
To John:
Can't understand your comments about copy constructor. There will be
definitely ONE instance/object of type class map. map is a map (or say a
playfield) for a game that will hold the parts of the playfield (the single
tiles of the map). So there is only one map consisting of maaaaany tiles.
Do i need the copy constructor in this case too? And why do i need it when i
never want to copy the map-object? Even if it is a good fashion or
recommended by default.
Of course i'm thankful for your hint. I will remember this for the objects
where i need more than one instance.
I'll check my code further to find who is deleting my tiles before i get
hand on it. Your comments are important for me that it is worth searching
for the bug further and not hunting C++ ghosts which every experienced
C++-Guru already knows.
Thx. again to you both and to John for maybe another little comment about
the copy constructor.
CU
Joerg Toellner
PS:
I already own Stroustroup C++ Language and Effective C++ and More effective
C++ as books. But you will understand, nobody can remember all the details
and s.t. it is like chasing an elephant with an microscope. You overseas the
obvious.
"John Harrison" <john_andronicus@hotmail.com> schrieb im Newsbeitrag
news:c1fqa1$1he3vq$1@ID-196037.news.uni-berlin.de...
>
> "Joerg Toellner" <toellner@oss-gmbh.de> wrote in message
> news:c1fpmi$pce$02$1@news.t-online.com...
> > 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.
>
> That's not true, you new them so you delete them.
>
> > 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?
> >
>
> 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?
> >
>
> What you've forgotten to do is write a copy constructor and assignment
> operator for your class.
>
> If you have two copies of the same object, then at present you will have
two
> copies of the same pointers. When the first object gets destructed
> everything is OK, but when the second gets destructed you are deleteing
> pointers that have been deleted already, so your program crashes.
>
> > Sorry for the dumb question, but i can't figure it out myself.
> >
>
> This is *the* classic newbie C++ gotcha. You need a good C++ book, any C++
> book that doesn't explain copy constructors is not worth reading.
>
>
> > TIA very much
> > Joerg Toellner
> >
>
> john
>
>
>
- Next message: John Harrison: "Re: confusing delete problem"
- Previous message: John Harrison: "Re: read problem"
- In reply to: John Harrison: "Re: confusing delete problem"
- Next in thread: John Harrison: "Re: confusing delete problem"
- Reply: John Harrison: "Re: confusing delete problem"
- Reply: Karl Heinz Buchegger: "Re: confusing delete problem"
- Reply: Thomas Matthews: "Re: confusing delete problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|