By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,856 Members | 1,986 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,856 IT Pros & Developers. It's quick & easy.

Check If Object Deleted

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.