473,287 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Check If Object Deleted

Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here's the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?

Thanks

Sep 29 '07 #1
14 17049
Mark wrote:
Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here's the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?
You can't, unless you either set the pointer to NULL after you delete
it, or the object's destructor sets some kind of static flag to indicate
it has been deleted.

--
Ian Collins.
Sep 29 '07 #2
Mark wrote:
Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here's the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?
a) You need not. The call to delete guarantees that the object is deleted.

b) Some people do

delete tetromino;
tetromino = 0;

However, note that if there is another pointer to *tetromino, and you delete
the pointee through that one, the test tetromino == 0 will buy you nothing
because tetromino will not magically be nulled. This idiom is dangerous at
best.

c) You can find / implement a smart pointer that reverts to 0 whenever the
pointee is delete through any of its handles.

_However_:

d) You should rethink your design. Your wish to check whether the pointer is
deleted is indicative that your headed the wrong way.
Best

Kai-Uwe Bux
Sep 29 '07 #3
"Mark" <mn*******@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here's the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?
delete does not change the pointer. Just where the pointer points. If you
want a pointer to point to NULL after you delete it, you need to do so
yourself.

delete tetromino;
tetromino = NULL;

if ( tetromino == NULL )
tetromino = new Tetromino;

It doesn't make much sense in this short piece of code, but it can in a
class heirarchy. I've sometimes used it for static variables too.

static jTexture* = NULL;
if ( jTexture = NULL )
{
// load texture
}

and in classes.

Also, it is perfectly safe to delete a NULL pointer. It's a non-op, nothing
happens. Deleting a pointer that's already been deleted however will cause
a crash.
Sep 29 '07 #4
On Sep 29, 6:36 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Mark" <mnbaya...@gmail.comwrote in message

news:11**********************@57g2000hsv.googlegro ups.com...
Hi,
I would like to check if my object has been deleted or not, but my
program keeps crashing. Here's the simplified code (in infinite loop)
delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;
This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?

delete does not change the pointer. Just where the pointer points. If you
want a pointer to point to NULL after you delete it, you need to do so
yourself.

delete tetromino;
tetromino = NULL;

if ( tetromino == NULL )
tetromino = new Tetromino;

It doesn't make much sense in this short piece of code, but it can in a
class heirarchy. I've sometimes used it for static variables too.

static jTexture* = NULL;
if ( jTexture = NULL )
{
// load texture

}

and in classes.

Also, it is perfectly safe to delete a NULL pointer. It's a non-op, nothing
happens. Deleting a pointer that's already been deleted however will cause
a crash.
Well, I was actually trying to get an object to kill itself (delete
this). Then in the main program I need to check if it's been deleted
or not. I don't think I could do something like this=NULL could I?

My other option is to use a global boolean (del=true) as I am now...
but I don't like the idea.

Oct 2 '07 #5
Mark wrote:
:: On Sep 29, 6:36 am, "Jim Langston" <tazmas...@rocketmail.com>
:: wrote:
::: "Mark" <mnbaya...@gmail.comwrote in message
:::
::: news:11**********************@57g2000hsv.googlegro ups.com...
:::
:::: Hi,
:::
:::: I would like to check if my object has been deleted or not, but
:::: my program keeps crashing. Here's the simplified code (in
:::: infinite loop)
:::
:::: delete tetromino;
:::: //if(tetromino==NULL)
:::: tetromino = new TetrominoI;
:::
:::: This continuously replaces the tetromino as expected, but if I
:::: take away the comment it crashes. How can I check if my object
:::: has been deleted or not?
:::
::: delete does not change the pointer. Just where the pointer
::: points. If you want a pointer to point to NULL after you delete
::: it, you need to do so yourself.
:::
::: delete tetromino;
::: tetromino = NULL;
:::
::: if ( tetromino == NULL )
::: tetromino = new Tetromino;
:::
::: It doesn't make much sense in this short piece of code, but it
::: can in a class heirarchy. I've sometimes used it for static
::: variables too.
:::
::: static jTexture* = NULL;
::: if ( jTexture = NULL )
::: {
::: // load texture
:::
::: }
:::
::: and in classes.
:::
::: Also, it is perfectly safe to delete a NULL pointer. It's a
::: non-op, nothing happens. Deleting a pointer that's already been
::: deleted however will cause a crash.
::
:: Well, I was actually trying to get an object to kill itself (delete
:: this). Then in the main program I need to check if it's been
:: deleted or not. I don't think I could do something like this=NULL
:: could I?
::

Why? Don't you trust your objects? :-)

Isn't the solution to just decide who is responsible for the
destruction? Then let the one responsible do what should be done.
Bo Persson


Oct 2 '07 #6
"Mark" <mn*******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
On Sep 29, 6:36 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Mark" <mnbaya...@gmail.comwrote in message

news:11**********************@57g2000hsv.googlegr oups.com...
Hi,
I would like to check if my object has been deleted or not, but my
program keeps crashing. Here's the simplified code (in infinite loop)
delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;
This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?

delete does not change the pointer. Just where the pointer points. If
you
want a pointer to point to NULL after you delete it, you need to do so
yourself.

delete tetromino;
tetromino = NULL;

if ( tetromino == NULL )
tetromino = new Tetromino;

It doesn't make much sense in this short piece of code, but it can in a
class heirarchy. I've sometimes used it for static variables too.

static jTexture* = NULL;
if ( jTexture = NULL )
{
// load texture

}

and in classes.

Also, it is perfectly safe to delete a NULL pointer. It's a non-op,
nothing
happens. Deleting a pointer that's already been deleted however will
cause
a crash.

Well, I was actually trying to get an object to kill itself (delete
this). Then in the main program I need to check if it's been deleted
or not. I don't think I could do something like this=NULL could I?

My other option is to use a global boolean (del=true) as I am now...
but I don't like the idea.
If an object does a delete this, whatever points to that object doesn't
change. That is, the pointer still points to where it was/is in memory. I
think if you wanted to use delete this then the method that did that should
return the fact if it was deleted or not so whatever called it could handle
it. Something like:

bool Foo::Check()
{
if ( something_or_other )
{
delete this;
return true;
}
else
return false;
}

Then in your mainline you could check the return value to see if the object
is still around.
Oct 2 '07 #7
On Oct 3, 1:35 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Mark" <mnbaya...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...
[snip]
If an object does a delete this, whatever points to that object doesn't
change. That is, the pointer still points to where it was/is in memory. I
think if you wanted to use delete this then the method that did that should
return the fact if it was deleted or not so whatever called it could handle
it. Something like:

bool Foo::Check()
{
if ( something_or_other )
{
delete this;
return true;
}
else
return false;

}

Then in your mainline you could check the return value to see if the object
is still around.
There is no need to do that. An object can be made to manage its own
resources
via its destructor.

This is a well known technique known as:

RAII (Resource Acquisition Is Initialization)

....which even the Standard C++ library make good use of.

--
Chris Val

Oct 2 '07 #8
On Oct 2, 9:15 am, "Chris ( Val )" <chris...@gmail.comwrote:
On Oct 3, 1:35 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Mark" <mnbaya...@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...

[snip]
If an object does a delete this, whatever points to that object doesn't
change. That is, the pointer still points to where it was/is in memory. I
think if you wanted to use delete this then the method that did that should
return the fact if it was deleted or not so whatever called it could handle
it. Something like:
bool Foo::Check()
{
if ( something_or_other )
{
delete this;
return true;
}
else
return false;
}
Then in your mainline you could check the return value to see if the object
is still around.

There is no need to do that. An object can be made to manage its own
resources
via its destructor.

This is a well known technique known as:

RAII (Resource Acquisition Is Initialization)

...which even the Standard C++ library make good use of.

--
Chris Val
Judging from the wikipedia article I just found, RAII is mainly used
for when an object goes out of scope. My object is global, and it
needs to be deleted before the program exits. I assume you mean that
I should do my handling inside the destructor then, but that doesn't
seem quite logical. When the object dies (tetris piece) it's supposed
to create a new one. I don't think objects should be creating
themselves. Anyway, the return true/false thing seems most
appropriate.

Thanks,
Mark

Oct 2 '07 #9
On Oct 2, 8:35 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Mark" <mnbaya...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...
On Sep 29, 6:36 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Mark" <mnbaya...@gmail.comwrote in message
>news:11**********************@57g2000hsv.googlegr oups.com...
Hi,
I would like to check if my object has been deleted or not, but my
program keeps crashing. Here's the simplified code (in infinite loop)
delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;
This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?
delete does not change the pointer. Just where the pointer points. If
you
want a pointer to point to NULL after you delete it, you need to do so
yourself.
delete tetromino;
tetromino = NULL;
if ( tetromino == NULL )
tetromino = new Tetromino;
It doesn't make much sense in this short piece of code, but it can in a
class heirarchy. I've sometimes used it for static variables too.
static jTexture* = NULL;
if ( jTexture = NULL )
{
// load texture
}
and in classes.
Also, it is perfectly safe to delete a NULL pointer. It's a non-op,
nothing
happens. Deleting a pointer that's already been deleted however will
cause
a crash.
Well, I was actually trying to get an object to kill itself (delete
this). Then in the main program I need to check if it's been deleted
or not. I don't think I could do something like this=NULL could I?
My other option is to use a global boolean (del=true) as I am now...
but I don't like the idea.

If an object does a delete this, whatever points to that object doesn't
change. That is, the pointer still points to where it was/is in memory. I
think if you wanted to use delete this then the method that did that should
return the fact if it was deleted or not so whatever called it could handle
it. Something like:

bool Foo::Check()
{
if ( something_or_other )
{
delete this;
return true;
}
else
return false;

}

Then in your mainline you could check the return value to see if the object
is still around.
Thanks for the suggestion! I guess I should have thought of that
myself.

Mark

Oct 2 '07 #10
"Chris ( Val )" <ch******@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
On Oct 3, 1:35 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Mark" <mnbaya...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegr oups.com...

[snip]
>If an object does a delete this, whatever points to that object doesn't
change. That is, the pointer still points to where it was/is in memory.
I
think if you wanted to use delete this then the method that did that
should
return the fact if it was deleted or not so whatever called it could
handle
it. Something like:

bool Foo::Check()
{
if ( something_or_other )
{
delete this;
return true;
}
else
return false;

}

Then in your mainline you could check the return value to see if the
object
is still around.

There is no need to do that. An object can be made to manage its own
resources
via its destructor.

This is a well known technique known as:

RAII (Resource Acquisition Is Initialization)

...which even the Standard C++ library make good use of.
Yes, but I'm pretty sure that Mark is talking about delete this before the
object goes out of scope and not in the destructor. Consider.

Foo Bar();
Bar.MaybeDeleteMe();

Now consider that DeleteMe does a delete this. Or not (for whatever
reason). Mainline has no clue if Bar is still good or not. If Bar's
instance was, in fact, deleted any attempt to use Bar's variables should
provoke undefined behavior. Even if Bar was a pointer.

Foo* Bar = new Foo();
Bar->MaybeDeleteMe();

If MaybeDeleteMe does a delete this or not, Bar will still point to some
memory location which will now be invalid. Again, any attempt to
dereference Bar will cause undefined behavior. Which is where the use of
the bool can come in.

Foo* Bar = new Foo();
if ( Bar->MaybeDeleteMe() )
Bar =NULL;

Now we would still have to check if Bar is a null pointer or not, but we
know if the instance is good or not.
Oct 3 '07 #11
On Oct 2, 6:07 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Chris ( Val )" <chris...@gmail.comwrote in messagenews:11**********************@r29g2000hsg.g ooglegroups.com...
On Oct 3, 1:35 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Mark" <mnbaya...@gmail.comwrote in message
>news:11**********************@g4g2000hsf.googlegr oups.com...
[snip]
If an object does a delete this, whatever points to that object doesn't
change. That is, the pointer still points to where it was/is in memory.
I
think if you wanted to use delete this then the method that did that
should
return the fact if it was deleted or not so whatever called it could
handle
it. Something like:
bool Foo::Check()
{
if ( something_or_other )
{
delete this;
return true;
}
else
return false;
}
Then in your mainline you could check the return value to see if the
object
is still around.
There is no need to do that. An object can be made to manage its own
resources
via its destructor.
This is a well known technique known as:
RAII (Resource Acquisition Is Initialization)
...which even the Standard C++ library make good use of.

Yes, but I'm pretty sure that Mark is talking about delete this before the
object goes out of scope and not in the destructor. Consider.

Foo Bar();
Bar.MaybeDeleteMe();

Now consider that DeleteMe does a delete this. Or not (for whatever
reason). Mainline has no clue if Bar is still good or not. If Bar's
instance was, in fact, deleted any attempt to use Bar's variables should
provoke undefined behavior. Even if Bar was a pointer.

Foo* Bar = new Foo();
Bar->MaybeDeleteMe();

If MaybeDeleteMe does a delete this or not, Bar will still point to some
memory location which will now be invalid. Again, any attempt to
dereference Bar will cause undefined behavior. Which is where the use of
the bool can come in.

Foo* Bar = new Foo();
if ( Bar->MaybeDeleteMe() )
Bar =NULL;

Now we would still have to check if Bar is a null pointer or not, but we
know if the instance is good or not.
That's precisely what I'm trying to do, except that I can put all the
"if deleted" code in place of Bar=NULL since I don't really need to
check it anywhere else.

Oct 3 '07 #12
On Oct 3, 11:07 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Chris ( Val )" <chris...@gmail.comwrote in messagenews:11**********************@r29g2000hsg.g ooglegroups.com...


On Oct 3, 1:35 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Mark" <mnbaya...@gmail.comwrote in message
>news:11**********************@g4g2000hsf.googlegr oups.com...
[snip]
>Ifanobjectdoes a delete this, whatever points to thatobjectdoesn't
change. That is, the pointer still points to where it was/is in memory.
I
thinkifyou wanted to use delete this then the method that did that
should
return the factifit wasdeletedor not so whatever called it could
handle
it. Something like:
bool Foo::Check()
{
if( something_or_other )
{
delete this;
return true;
}
else
return false;
}
Then in your mainline you couldcheckthe return value to seeifthe
object
is still around.
There is no need to do that. Anobjectcan be made to manage its own
resources
via its destructor.
This is a well known technique known as:
RAII (Resource Acquisition Is Initialization)
...which even the Standard C++ library make good use of.

Yes, but I'm pretty sure that Mark is talking about delete this before theobjectgoes out of scope and not in the destructor. Consider.
[snip]

Thats fine.

I wasn't entriely sure of the OP's design and or requirements.

I mentioned it because there are times when an object needs to
manage its own resources, and this was one good way of doing it.

Using a template, the RAII idiom, and a little thought in design,
you could implement your own version of an "auto pointer", etc...

--
Chris Val

Oct 3 '07 #13
"Mark" <mn*******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On Oct 2, 9:15 am, "Chris ( Val )" <chris...@gmail.comwrote:
>On Oct 3, 1:35 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Mark" <mnbaya...@gmail.comwrote in message
>news:11**********************@g4g2000hsf.googlegr oups.com...

[snip]
If an object does a delete this, whatever points to that object doesn't
change. That is, the pointer still points to where it was/is in
memory. I
think if you wanted to use delete this then the method that did that
should
return the fact if it was deleted or not so whatever called it could
handle
it. Something like:
bool Foo::Check()
{
if ( something_or_other )
{
delete this;
return true;
}
else
return false;
}
Then in your mainline you could check the return value to see if the
object
is still around.

There is no need to do that. An object can be made to manage its own
resources
via its destructor.

This is a well known technique known as:

RAII (Resource Acquisition Is Initialization)

...which even the Standard C++ library make good use of.

--
Chris Val

Judging from the wikipedia article I just found, RAII is mainly used
for when an object goes out of scope. My object is global, and it
needs to be deleted before the program exits. I assume you mean that
I should do my handling inside the destructor then, but that doesn't
seem quite logical. When the object dies (tetris piece) it's supposed
to create a new one. I don't think objects should be creating
themselves. Anyway, the return true/false thing seems most
appropriate.
I do it a bit differently in my code. I have a vector for beams that can go
out of range and need to be deleted. I handle it in the class and in
mainline:

// Update ALL the -On-The-Fly- or Fired 'Beams' to NEW positions.
for ( BeamEffectsType::iterator it = Client.BeamEffects.begin(); it !=
Client.BeamEffects.end(); )
{
if ( !(*it).second->Update() )
{
delete (*it).second;
it = Client.BeamEffects.erase(it);
}
else
++it;
}

I do not have Update do a delete this, but isntead return a boolean stating
if the beam should be deleted or not. I could, if I wished, wrap this code
in a class that contains the vector of beams if I wished. I just haven't
chosen to do that (yet).
Oct 3 '07 #14
After being deleted, the vlaue of the pointer tetromino should remain the
same, however, the memory that the pointer point to has been freed.
"Mark" <mn*******@gmail.com>
??????:11**********************@57g2000hsv.googleg roups.com...
Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here's the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?

Thanks

Oct 4 '07 #15

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

Similar topics

2
by: Helmut Jarausch | last post by:
Hi, sorry, this seems to be a FAQ but I couldn't find anything I need to check if an object is a compiled regular expression Say import re RX= re.compile('^something') how to test
10
by: bolnvhuis | last post by:
I'm using an STL map in my code. My application sometimes tries to delete things twice from the map. This leads to a crash in my current code. The problem is probably the way I check whether it is...
3
by: tshad | last post by:
If I create an object (instantiate) then do it again using the same Pointer, will I leave the old object hanging around wasting memory? Would this create a memory leak or would it be taken care...
4
by: Mark Berry | last post by:
Hi, I'm working on my "last resort" error block for a web application. If the error occurs after a Request has been made, I want to show the URL. If the Request object is not available, I'll...
5
by: futileissue | last post by:
Beginner, so please bare with me. I'm not sure what to call what it is I'm looking for. If I have an object class, let's call it "Creature": class Creature: def __init__(self, status):...
0
by: Edwin.Madari | last post by:
updated creature running in its own thread will get you started. try it foryourself, change sleep times per your need. import os, sys, threading, time class Creature: def __init__(self,...
2
by: shariquehabib | last post by:
Hi, Any one let me know that how can we check that who deleted files in unix (sun). Thanks, Sharique
7
by: ismailc | last post by:
Good day, I thought i had this sorted but it does not seem to check if the object is undefined / not created. I'm trying to check if object is undefined & i tried a few methos but no luck...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.