 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
penny336 Guest
|
Posted: Thu Mar 04, 2004 8:26 am Post subject: prevent delete again |
|
|
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"<
if (sizeof(street) >0 )
{
cout <<"Delete Street"<
delete [] street;
street = NULL;
}
if (city !=NULL)
{
cout <<"Delete city"<
delete [] city;
city = NULL;
}
}
|
|
| Back to top |
|
 |
Patrik Stellmann Guest
|
Posted: Thu Mar 04, 2004 9:06 am Post subject: Re: prevent delete again |
|
|
penny336 wrote:
| Quote: | 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"<
if (sizeof(street) >0 )
{
cout <<"Delete Street"<
delete [] street;
street = NULL;
}
if (city !=NULL)
{
cout <<"Delete city"<
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...
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Mar 04, 2004 6:41 pm Post subject: Re: prevent delete again |
|
|
"penny336" <penny336 (AT) hotmail (DOT) com> wrote
| Quote: | 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"<
if (sizeof(street) >0 )
{
cout <<"Delete Street"<
delete [] street;
street = NULL;
}
if (city !=NULL)
{
cout <<"Delete city"<
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
|
|
| Back to top |
|
 |
Tom Plunket Guest
|
Posted: Fri Mar 05, 2004 1:16 am Post subject: Re: prevent delete again |
|
|
penny336 wrote:
| Quote: | 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!
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|