473,564 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deleting an object twice?

Hi,

Does deleting an object more than one times incur undefined behavior? I
think it doesn't but just making sure... thanks

Rick

Jul 22 '05 #1
15 3578
Rick wrote:

Hi,

Does deleting an object more than one times incur undefined behavior?
The first delete renders the pointer to it invalid. Using an
invalid pointer invokes undefined behaviour.
I
think it doesn't but just making sure... thanks


You are wrong.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
"Rick" <rrquick@nosp am-com> wrote in message
news:3f******** @clarion.carno. net.au...
Hi,

Does deleting an object more than one times incur undefined behavior? I
think it doesn't but just making sure... thanks

Rick


Are you setting your pointer to null after the first delete? I think you
have deleting a null pointer mixed up with deleting a pointer twice. I am
not sure if this is a standard behavior or not but on the few compilers that
I have worked with delete checks for a null pointer and does nothing.
Jul 22 '05 #3
Rick wrote:
Hi,

Does deleting an object more than one times incur undefined behavior? I
think it doesn't but just making sure... thanks

Rick


It does. Delete renders a pointer to be invalid and so calling delete again
will result in undefined behaviour. To circumvent this, simply set the
pointer to NULL after deletion. Calling delete for a NULL pointer will do
nothing.

--
Dipl.-Inform. Hendrik Belitz
Central Laboratory of Electronics
Research Center Juelich
Jul 22 '05 #4
On Fri, 05 Dec 2003 03:08:58 +1100, Rick <rrquick@nosp am-com> wrote:
Hi,

Does deleting an object more than one times incur undefined behavior? I
think it doesn't but just making sure... thanks


int* p = new int;
delete p;
delete p; //undefined behaviour

Generally double deletes cause heap corruption and, if you're lucky, a
crash. You can of course delete null pointers as often as you like.
e.g.

int* p = new int;
delete p;
p = 0;
delete p; //fine

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #5
> Does deleting an object more than one times incur undefined behavior? I
think it doesn't but just making sure... thanks


How can you possibly delete an object more than once? Once you have deleted
it, it no longer exists, so what are you deleting the second time?

If you are thinking of something like this:

int* p = new int;
delete p;
delete p;

then after the first "delete p", p is an invalid pointer -- the only thing
you can do with p is give it a new value.

So there is no question of deleting "an object" twice.
Jul 22 '05 #6
Andrew Koenig wrote:
Does deleting an object more than one times incur undefined behavior?
I think it doesn't but just making sure... thanks
How can you possibly delete an object more than once?
Once you have deleted it, it no longer exists,
so what are you deleting the second time?

If you are thinking of something like this:

int* p = new int;
delete p;
delete p;

then after the first "delete p", p is an invalid pointer --


I object to your abuse of the English language.
The pointer p is *not* deleted.
The *object* to which pointer p points is deleted.
Pointer p is a valid pointer to an invalid object
(of type int in this case) because delete calls
the object's destructor to destroy the object.
the only thing you can do with p is give it a new value.

So there is no question of deleting "an object" twice.


The problem is that the second delete
is almost certainly a programming error -- a bug.
If you simply set the pointer p = NULL,
the bug will go undetected.
You can't do anything about this for built-in types
but you can for User Defined Types (UDTs):

class X {
private:
int Valid;
// other data members
public:
X(void) Valid(0x5555555 5) { }
~X(void) {
if (0x55555555 == Valid)
Valid == 0xaaaaaaaa;
else
std::cerr << "Invalid object of type X!" << std::endl;
}
// other public functions and operators
};

Jul 22 '05 #7
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:3F******** ******@jpl.nasa .gov...
Andrew Koenig wrote:
Does deleting an object more than one times incur undefined behavior?
I think it doesn't but just making sure... thanks
How can you possibly delete an object more than once?
Once you have deleted it, it no longer exists,
so what are you deleting the second time?

If you are thinking of something like this:

int* p = new int;
delete p;
delete p;

then after the first "delete p", p is an invalid pointer --


I object to your abuse of the English language.
The pointer p is *not* deleted.


Who said it was?
The *object* to which pointer p points is deleted.
As far as I can see, neither the OP nor AK have stated or implied that it is
the pointer being deleted. Only the C++ source code says that :-)
Pointer p is a valid pointer to an invalid object
(of type int in this case) because delete calls
the object's destructor to destroy the object.
the only thing you can do with p is give it a new value.

So there is no question of deleting "an object" twice.


The problem is that the second delete
is almost certainly a programming error -- a bug.
If you simply set the pointer p = NULL,
the bug will go undetected.


I'd say that would fix the bug rather than hide it, albeit unsatisfactoril y.
And in some circumstances two deletes are quite normal. An object's member
pointer to a valid object might or might not have been deleted and assigned
to null before the destructor is reached, so you would like the destructor
to delete it regardless.

DW

Jul 22 '05 #8
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:3F******** ******@jpl.nasa .gov...
Andrew Koenig wrote:
Does deleting an object more than one times incur undefined behavior?
I think it doesn't but just making sure... thanks
How can you possibly delete an object more than once?
Once you have deleted it, it no longer exists,
so what are you deleting the second time?

If you are thinking of something like this:

int* p = new int;
delete p;
delete p;

then after the first "delete p", p is an invalid pointer --


I object to your abuse of the English language.
The pointer p is *not* deleted.


I don't think Andrew said so; he just said: "after the first "delete p"". He
didn't say that the pointer was deleted.
The *object* to which pointer p points is deleted.
Pointer p is a valid pointer to an invalid object
(of type int in this case) because delete calls
the object's destructor to destroy the object.
the only thing you can do with p is give it a new value.

So there is no question of deleting "an object" twice.
The problem is that the second delete
is almost certainly a programming error -- a bug.
If you simply set the pointer p = NULL,
the bug will go undetected.


It is not necessarilly a bug. There is a reason why you can do a delete on a
null pointer.
You can't do anything about this for built-in types
but you can for User Defined Types (UDTs):

class X {
private:
int Valid;
// other data members
public:
X(void) Valid(0x5555555 5) { }
~X(void) {
if (0x55555555 == Valid)
Valid == 0xaaaaaaaa;
else
std::cerr << "Invalid object of type X!" << std::endl;
}
// other public functions and operators
};


Even though this technique may work on some platforms with some compilers
under certain circumstances, it is not *guaranteed* to work. Once an object
has been deleted, the memory it occupied may have been overwritten, unmapped
(resulting in a page fault) or occupied by another object (possibly of the
same class). There is also a chance the vtable pointer of the deleted object
is damaged; if the pointer is used to call any virtual member function
(including the destructor) on the deleted object very weird things may
happen - good for hours of debugging fun.

Personnally I prefer resetting the pointer to NULL after deleting the object
it is pointing to. That way it easy to detect if the pointer is still
pointing to a valid object, and on most platforms leads to very predictable
behaviour when that pointer is accidentally dereferenced. Resetting the
pointer does not conflict with using a member variable to see if the object
is still valid. Even tough that technique is not 100% reliable, it increases
the chance that code that shouldn't work doesn't work (which is usually a
good thing). Member functions (not just the destructor) can use the member
variable with the magic number as a precondition. This technique can be
especially useful to detect lifetime issues i.c.w. having multiple pointers
to the same object.
--
Peter van Merkerk
peter.van.merke rk(at)dse.nl


Jul 22 '05 #9
How can you possibly delete an object more than once?
Once you have deleted it, it no longer exists,
so what are you deleting the second time?

If you are thinking of something like this:

int* p = new int;
delete p;
delete p;

then after the first "delete p", p is an invalid pointer --
I object to your abuse of the English language.


That's your prerogative. Mine is to say that you're mistaken :-)
The pointer p is *not* deleted.
I never said it was.
The *object* to which pointer p points is deleted.
Yes, that's what I said.
Pointer p is a valid pointer to an invalid object
(of type int in this case) because delete calls
the object's destructor to destroy the object.
Wrong. Once you have said "delete p", the value of p is now invalid. For
example, in

int* p = new int;
delete p;
int* q = p;

the attempt to copy p in order to initialize q yields undefined behavior, so
the implementation can do as it pleases.
the only thing you can do with p is give it a new value. So there is no question of deleting "an object" twice.

The problem is that the second delete
is almost certainly a programming error -- a bug.
Yes, indeed -- I never said otherwise.
If you simply set the pointer p = NULL,
the bug will go undetected.
Yes indeed -- I never said otherwise.
You can't do anything about this for built-in types
but you can for User Defined Types (UDTs):

class X {
private:
int Valid;
// other data members
public:
X(void) Valid(0x5555555 5) { }
~X(void) {
if (0x55555555 == Valid)
Valid == 0xaaaaaaaa;
else
std::cerr << "Invalid object of type X!" << std::endl;
}
// other public functions and operators
};


And your point is what, exactly? Are you claiming that C++ requires some
particular behavior in the case of

X* p = new X;
delete p;
delete p;

It doesn't; the implementation can do as it pleases.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2027
by: Thomas Philips | last post by:
I have a question about deleting objects. My game has two classes, Player and Alien, essentially identical, instances of which can shoot at each other. Player is described below class Player(object): #Class attributes for class Player n=0 #n is the number of players #Private methods for class Player def __init__(self,name):
6
2425
by: Matan Nassau | last post by:
Hello. i have a composite which i want to delete. this is a composite which represents a boolean expression (see a previous post of mine with more details at...
9
1871
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that besides using pointers rather...
12
1941
by: Christoff Pale | last post by:
Hi, suppose I have a list of strings; I have to iterate of the strings and delete certain entries. I and not sure how to do this? for example: #include<list> #include<string> #include<iostream> int main(){
4
14002
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
5
2346
by: Patrick Vanden Driessche | last post by:
Hi All, I'm currently writing an in-house Form validation framework (WinForms) which is based on 'Component'-inheriting object. So basically, I have a small hierarchy. FormValidator +-- ControlValidator +--ValidationRule
6
9556
by: belmontpress | last post by:
I wish to delete some files from a directory after reading them but have the problem that the system says that the files are in use and cannot delete them even though I have set the StreamReader to null: DirectoryInfo di = new DirectoryInfo(@"C:\test\"); FileInfo fiArray = di.GetFiles("*.txt"); foreach (FileInfo fi in fiArray) {
51
10258
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
12
2335
by: mc | last post by:
Hi Experts, This may be obvious but I can't find anything one way or another. We have a class which is always allocated using new, e.g. Foo* foo = new Foo() so we came up with the idea of releasing the memory from the destructor as follows: Foo::Foo() { // Initialize stuff
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.