Re: confusing delete problem

From: Joerg Toellner (toellner_at_oss-gmbh.de)
Date: 02/24/04


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
>
>
>



Relevant Pages

  • Re: confusing delete problem
    ... > When i construct a new instance of class map and later delete it again my ... > program crash at the delete command in the destructor. ... What you've forgotten to do is write a copy constructor and assignment ... copies of the same pointers. ...
    (comp.lang.cpp)
  • Re: Use the accessor! Was: Copy Constructor Usage
    ... I think you are getting too hung up on the copy constructor bit. ... accessor fns on some bogus efficiency argument. ... a field 'leng' might map onto an accessor 'size'; ... No style or design rules give us a free lunch. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: confusing delete problem
    ... Joerg Toellner wrote: ... > thx for your replies. ... > Can't understand your comments about copy constructor. ... > definitely ONE instance/object of type class map. ...
    (comp.lang.cpp)
  • Re: Free called instead of delete
    ... virtual destructor or B overrides the virtual destructor for A. ... As for the mem leak, if you look at the cstor of std::map, within it ... dstor would destroy the hidden default value type object. ... When a std::map is cleared, destructors are called for the elements of the map. ...
    (microsoft.public.vc.mfc)
  • Re: Get list of unique words in a string
    ... may declare/define the same identifiers. ... additional "namespace" for all libraries of PointedEars' JavaScript ... Why do you declare functions in Map which don't use any of map ... _Value.isInstance and few others could all be taken out of constructor ...
    (comp.lang.javascript)