struct Obj
{
/*code omitted*/
Obj* pNextOne;
}

struct DObj : public Obj
{
int sth; //or whatever
}

(:
Im building a list of Obj (so that each has a pointer to
next one), but i'd like some of them to be DObj,
everything is fine, but when i delete the list sth goes
wrong, when im deleting any DObj. Could someone give me a
hint how to delete such a list ????
thanks

lists by Kyle

Kyle
Sun Dec 14 08:02:31 CST 2003

if it helps

app says:

DAMAGE: after Normal block (#25) at 0x1320c30

compiler says:

memory check error at 0x01320C74 = 0x00, should be 0xFD.
memory check error at 0x01320C75 = 0x00, should be 0xFD.
memory check error at 0x01320C76 = 0x00, should be 0xFD.
memory check error at 0x01320C77 = 0x00, should be 0xFD.

Re: lists by Robert

Robert
Sun Dec 14 14:30:52 CST 2003

Change the structs to classes and put an empty virtual destructor into
the class Obj:

class Obj
{
public:
~Obj() {}
/* code omitted */
Obj* pNextOne;
};

class DObj: public Obj
{
int sth;
}

Robert-Antonio

--------------------------------------------------------
To obtain my real email address, please remove the
'b' letter from it :)
--------------------------------------------------------

On Sun, 14 Dec 2003, Kyle wrote:

> struct Obj
> {
> /*code omitted*/
> Obj* pNextOne;
> }
>
> struct DObj : public Obj
> {
> int sth; //or whatever
> }
>
> (:
> Im building a list of Obj (so that each has a pointer to
> next one), but i'd like some of them to be DObj,
> everything is fine, but when i delete the list sth goes
> wrong, when im deleting any DObj. Could someone give me a
> hint how to delete such a list ????
> thanks
>

Re: lists by Lucas

Lucas
Mon Dec 15 10:16:09 CST 2003

Hi,
use a virtual destructor.

Lucas/

"Kyle" <anonymous@discussions.microsoft.com> wrote in message
news:051801c3c246$29730950$a301280a@phx.gbl...
> struct Obj
> {
> /*code omitted*/
> Obj* pNextOne;
> }
>
> struct DObj : public Obj
> {
> int sth; //or whatever
> }
>
> (:
> Im building a list of Obj (so that each has a pointer to
> next one), but i'd like some of them to be DObj,
> everything is fine, but when i delete the list sth goes
> wrong, when im deleting any DObj. Could someone give me a
> hint how to delete such a list ????
> thanks



Re: lists by Kyle

Kyle
Mon Dec 15 12:38:53 CST 2003

>-----Original Message-----
>Hi,
>use a virtual destructor.
>
>Lucas/

But what, does it do ... I mean ive added one, but it's of
no use, perhaps i dont understand sth (im getting other
error though :),
Am i supposed to make a destructor in a child object,
which will delete additional variables ???
My list is deleted in a destructor of another class -
Manager. Ive tried almost everything (i think) - i mean
lots of casting (ive included variable in base object
which identifies the type of object, then i cast the
object to its (proper)type (child or base) and delete it,

Re: lists by Lucas

Lucas
Mon Dec 15 13:39:11 CST 2003

Hi,
You have to declare the virtual destructor in the base class, this way, the
derived class will have a virtual destructor too.
So, when you do

class base {
public:
~base () {}
};

class derived : public base {
public:
~derived () {}
};


base *b = new derived;
delete b;
/*
now, this will invoke the derived destructor (and this last the base
destructor), but if you do not have a virtual destructor, you will invoke
the base destructor and leek memory.
*/

Lucas/

"Kyle" <anonymous@discussions.microsoft.com> wrote in message
news:023c01c3c33a$b0ca3ff0$a301280a@phx.gbl...
> >-----Original Message-----
> >Hi,
> >use a virtual destructor.
> >
> >Lucas/
>
> But what, does it do ... I mean ive added one, but it's of
> no use, perhaps i dont understand sth (im getting other
> error though :),
> Am i supposed to make a destructor in a child object,
> which will delete additional variables ???
> My list is deleted in a destructor of another class -
> Manager. Ive tried almost everything (i think) - i mean
> lots of casting (ive included variable in base object
> which identifies the type of object, then i cast the
> object to its (proper)type (child or base) and delete it,



Re: lists by Kyle

Kyle
Mon Dec 15 13:43:44 CST 2003

actually iv menaged to solve the prob, the problem was i ve biult the list
incorectly so i couldn delete it in good way (:

Thanks for attantion.



Re: lists by Douglas

Douglas
Mon Dec 15 13:46:15 CST 2003

class Base
{
char* b1;
~Base()
{
delete b1;
}
};

class Derived : public Base
{
char* d1;
~Derived()
{
delete d1;
}
};

Derived * pd = new Derived;
delete pd;

In the above case, both d1 and b1 get deleted in the destructors.

Base * pb = new Derived; // notice the base type pointer
delete pb;

In above case, only b1 gets deleted as the destructor for Derived is not
called.
If the Base destructor had been declared as virtual, than both destructors
get called regardless of which type is used to delete the object.


"Kyle" <anonymous@discussions.microsoft.com> wrote in message
news:023c01c3c33a$b0ca3ff0$a301280a@phx.gbl...
> >-----Original Message-----
> >Hi,
> >use a virtual destructor.
> >
> >Lucas/
>
> But what, does it do ... I mean ive added one, but it's of
> no use, perhaps i dont understand sth (im getting other
> error though :),
> Am i supposed to make a destructor in a child object,
> which will delete additional variables ???
> My list is deleted in a destructor of another class -
> Manager. Ive tried almost everything (i think) - i mean
> lots of casting (ive included variable in base object
> which identifies the type of object, then i cast the
> object to its (proper)type (child or base) and delete it,



Re: lists by James

James
Tue Dec 16 16:28:49 CST 2003

At this point, in this thread, THREE people have now post code in an
attempt to solve this man's problem, and NONE OF THEM have actually made the
destructor virtual. (You at least point out that you code is wrong, but it
would be nice if you also included the correct code.....)

struct Obj
{
/*code omitted*/
Obj* pNextOne;
virtual ~Obj() {} // nothing needed in body for the code shown. No
changes needed elsewhere.
}


--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Douglas Peterson" <Tergiver@nospam.msn.com> wrote in message
news:uEhgbQ0wDHA.1196@TK2MSFTNGP12.phx.gbl...
> class Base
> {
> char* b1;
> ~Base()
> {
> delete b1;
> }
> };
>
> class Derived : public Base
> {
> char* d1;
> ~Derived()
> {
> delete d1;
> }
> };
>
> Derived * pd = new Derived;
> delete pd;
>
> In the above case, both d1 and b1 get deleted in the destructors.
>
> Base * pb = new Derived; // notice the base type pointer
> delete pb;
>
> In above case, only b1 gets deleted as the destructor for Derived is not
> called.
> If the Base destructor had been declared as virtual, than both destructors
> get called regardless of which type is used to delete the object.
>
>
> "Kyle" <anonymous@discussions.microsoft.com> wrote in message
> news:023c01c3c33a$b0ca3ff0$a301280a@phx.gbl...
> > >-----Original Message-----
> > >Hi,
> > >use a virtual destructor.
> > >
> > >Lucas/
> >
> > But what, does it do ... I mean ive added one, but it's of
> > no use, perhaps i dont understand sth (im getting other
> > error though :),
> > Am i supposed to make a destructor in a child object,
> > which will delete additional variables ???
> > My list is deleted in a destructor of another class -
> > Manager. Ive tried almost everything (i think) - i mean
> > lots of casting (ive included variable in base object
> > which identifies the type of object, then i cast the
> > object to its (proper)type (child or base) and delete it,
>
>



Re: lists by Kyle

Kyle
Thu Dec 18 05:07:39 CST 2003

Thanks a lot. (: