prevent delete again

P

penny336

dear all,
i am using vc++ 6.0 sp5
i have a class called Address,it allocated some memory space for streetname
and city
i first define it as

Address add = new Address("Madrian","UK");
...
delete add // delete it explicitly

end of program
but my program get error that before the end of program the destructor is
called again and try to delete the street again,
how to prevent action, i have tried to use sizeof (street) or !=null to
check in if loop, but it still can jump and try to delete street again
it is not i want, please give me solution.
here is my destructor
Address::~Address () {
cout << "address destructor is called"<<endl;
if (sizeof(street) >0 )
{
cout <<"Delete Street"<<endl;
delete [] street;
street = NULL;
}

if (city !=NULL)
{
cout <<"Delete city"<<endl;
delete [] city;
city = NULL;
}
}
 
P

Patrik Stellmann

penny336 said:
dear all,
i am using vc++ 6.0 sp5
i have a class called Address,it allocated some memory space for streetname
and city
i first define it as

Address add = new Address("Madrian","UK");
..
delete add // delete it explicitly

end of program
but my program get error that before the end of program the destructor is
called again and try to delete the street again,
how to prevent action, i have tried to use sizeof (street) or !=null to
check in if loop, but it still can jump and try to delete street again
it is not i want, please give me solution.
here is my destructor
Address::~Address () {
cout << "address destructor is called"<<endl;
if (sizeof(street) >0 )
{
cout <<"Delete Street"<<endl;
delete [] street;
street = NULL;
}

if (city !=NULL)
{
cout <<"Delete city"<<endl;
delete [] city;
city = NULL;
}
}
sizeof(street) will return the size of the pointer - not the size of the
memory it points to. just do it like you did for city - check if it's
null. actually you con't need the check since a delete on NULL has no
effect. Of yourse you always need to set the pointer to NULL when you
deleted it!
Even easier would be to use std::string instead of doing the
memory-handling yourself...
 
J

John Harrison

penny336 said:
dear all,
i am using vc++ 6.0 sp5
i have a class called Address,it allocated some memory space for streetname
and city
i first define it as

Address add = new Address("Madrian","UK");
..
delete add // delete it explicitly

end of program
but my program get error that before the end of program the destructor is
called again and try to delete the street again,
how to prevent action, i have tried to use sizeof (street) or !=null to
check in if loop, but it still can jump and try to delete street again
it is not i want, please give me solution.
here is my destructor
Address::~Address () {
cout << "address destructor is called"<<endl;
if (sizeof(street) >0 )
{
cout <<"Delete Street"<<endl;
delete [] street;
street = NULL;
}

if (city !=NULL)
{
cout <<"Delete city"<<endl;
delete [] city;
city = NULL;
}
}

Neither of those will work. When your object is destructed its gone, there's
no point setting street and city to NULL since the object is about to
disappear.

Since you didn't post a complete program its hard to say what it wrong with
your code, but I suspect that what you have done wrong is fail to define a
copy constructor and assignment operator for your class, does that ring any
bells? Get out your favourite C++ book and read why a class with a
destructor must also have a copy constructor and assignment operator.

john
 
T

Tom Plunket

penny336 said:
Address add = new Address("Madrian","UK");
..
delete add // delete it explicitly

end of program
but my program get error that before the end of program the destructor is
called again and...

Welcome to C++. That works fine in Java, but not in C++. In
fact, that's not even valid C++ code.

In C++, if you want an "automatic" variable, don't do the 'new'
thing. Just write it like:

Address add("Madrian", "UK");

....Then, you don't ever need to delete this object, because it is
"automatically" cleaned up. The destructor is called when the
variable goes out of scope, that is, when it hits the closest
enclosing close brace.

If you want to manage the memory yourself, then you need to keep
a pointer to what you're allocating:

Address *add = new...
// ...
delete add;

See the asterisk in there? That star means, "this thing is a
pointer. I'm going to control allocation and deallocation
myself."

If neither of these things help you out, post the actual code
that you're working on. Rather, post the smallest subset of the
code that compiles and runs and doesn't do what you want. (In
other words, strip it down so that JUST the behavior you're
talking about is happening.)

-tom!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top