Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > test to see if object exists with new/delete usage

Reply
Thread Tools

test to see if object exists with new/delete usage

 
 
Squid Seven
Guest
Posts: n/a
 
      07-07-2005
I create a pointer to an item:

CardSession *cardSession;

Then, later, I use new to create an instance of the item and assign it
to that pointer:

cardSession = new CardSession();

In another function, I want to test if an object is assigned to that
pointer, and delete the object if that is so:

if( cardSession )
delete cardSession;

I thought this would work, but then I discovered that, if cardSession
had *ever* been assigned a CardSession object with new, then the if
statement executes the delete command, even if the object had since been
deleted. Thus, I sometimes get an item doubly deleted with this code -
an undefined behavior that I think is causing my program to crash. Note
that if I have not yet used new in the program to assign an object to
cardSession, the if statement does not execute.

Could somebody explain this to me? Am I not understanding pointer usage
correctly, or is it innapropriate usage of new/delete? How can I do
this in an efficient fashion, without managing a global pointer to be
modified everytime I create or delete an object?
 
Reply With Quote
 
 
 
 
Squid Seven
Guest
Posts: n/a
 
      07-07-2005
Squid Seven wrote:
> I create a pointer to an item:
>
> CardSession *cardSession;
>
> Then, later, I use new to create an instance of the item and assign it
> to that pointer:
>
> cardSession = new CardSession();
>
> In another function, I want to test if an object is assigned to that
> pointer, and delete the object if that is so:
>
> if( cardSession )
> delete cardSession;
>
> I thought this would work, but then I discovered that, if cardSession
> had *ever* been assigned a CardSession object with new, then the if
> statement executes the delete command, even if the object had since been
> deleted. Thus, I sometimes get an item doubly deleted with this code -
> an undefined behavior that I think is causing my program to crash. Note
> that if I have not yet used new in the program to assign an object to
> cardSession, the if statement does not execute.
>
> Could somebody explain this to me? Am I not understanding pointer usage
> correctly, or is it innapropriate usage of new/delete? How can I do
> this in an efficient fashion, without managing a global pointer to be
> modified everytime I create or delete an object?


meant to say "without managing a global *flag* to be..." - sorry
 
Reply With Quote
 
 
 
 
Matthias Kluwe
Guest
Posts: n/a
 
      07-07-2005
> I create a pointer to an item:
>
> CardSession *cardSession;


Note that the pointer may not have NULL value here.

> [...]


> In another function, I want to test if an object is assigned to that
> pointer, and delete the object if that is so:


> if( cardSession )
> delete cardSession;


This does not test if the pointer points to an object.

> I thought this would work, but then I discovered that, if cardSession
> had *ever* been assigned a CardSession object with new, then the if
> statement executes the delete command, even if the object had since been
> deleted. Thus, I sometimes get an item doubly deleted with this code -
> an undefined behavior that I think is causing my program to crash. Note
> that if I have not yet used new in the program to assign an object to
> cardSession, the if statement does not execute.


Depends on cardSessions's value obviously. You may explicitly set the
pointer to NULL initially or after deleting the pointed to object. It's
safe to call delete cardSession when cardSession==NULL.

Regards,
Matthias

 
Reply With Quote
 
Chad J McQuinn
Guest
Posts: n/a
 
      07-07-2005
Squid Seven wrote:
> I create a pointer to an item:
>
> CardSession *cardSession;


Change this to

CardSession *cardSession = null;

It's good practice to initialize the variable.

> if( cardSession )
> delete cardSession;
>
> I thought this would work, but then I discovered that, if cardSession
> had *ever* been assigned a CardSession object with new, then the if
> statement executes the delete command, even if the object had since been
> deleted.


Yes. Either you don't understand what if(cardSession) does, or you don't
understand what delete does. For a pointer type such as the variable you
have here, the conditional

if(cardSession)

is a check to see whether cardSession is a null pointer or not--it does
NOT check if the pointed-to object is valid (and there is no way in
general to perform such a check in standard C++).

The subsequent delete statement destroys whatever is pointed to by
cardSession. If a valid object isn't there, you are likely to crash.

What you need to do here, and what is also good practice, is to add this
after you delete:

cardSession = null;

Also be aware that your check for whether cardSession is null is not
necessary. It is perfectly safe to delete a null pointer (but not an
invalid one), so just

delete cardSession;
cardSession=null;

in place of your original "if" will do the job.

-cjm
 
Reply With Quote
 
Squid Seven
Guest
Posts: n/a
 
      07-07-2005
Matthias Kluwe wrote:
>>I create a pointer to an item:
>>
>> CardSession *cardSession;

>
>
> Note that the pointer may not have NULL value here.
>
>
>>[...]

>
>
>>In another function, I want to test if an object is assigned to that
>>pointer, and delete the object if that is so:

>
>
>> if( cardSession )
>> delete cardSession;

>
>
> This does not test if the pointer points to an object.
>
>
>>I thought this would work, but then I discovered that, if cardSession
>>had *ever* been assigned a CardSession object with new, then the if
>>statement executes the delete command, even if the object had since been
>>deleted. Thus, I sometimes get an item doubly deleted with this code -
>>an undefined behavior that I think is causing my program to crash. Note
>>that if I have not yet used new in the program to assign an object to
>>cardSession, the if statement does not execute.

>
>
> Depends on cardSessions's value obviously. You may explicitly set the
> pointer to NULL initially or after deleting the pointed to object. It's
> safe to call delete cardSession when cardSession==NULL.
>
> Regards,
> Matthias
>

OK - let me see if I follow... When I create the pointer, if I don't
initialize it to something, I don't know what it's pointing at - could
be null, could not be. Then, when I delete the object associated with
the pointer, while I am indicating that the memory pointed at is
available for other use, the pointer still points at that memory, until
assigned to something else, and so causes the if statement to execute
it's contents?

Also, if this does not test if the pointer points to an object, how do I
test for it?

Thanks for your time Matthias
 
Reply With Quote
 
Me
Guest
Posts: n/a
 
      07-07-2005


Squid Seven wrote:
> I create a pointer to an item:
>
> CardSession *cardSession;
>
> Then, later, I use new to create an instance of the item and assign it
> to that pointer:
>
> cardSession = new CardSession();
>
> In another function, I want to test if an object is assigned to that
> pointer, and delete the object if that is so:
>
> if( cardSession )
> delete cardSession;


Unless you overloaded a new operator to have a throw() somewhere, the
default one doesn't return null on failure, it throws bad_alloc. Also,
you don't need to check for null when you delete, the delete operator
does that check for you.

> I thought this would work, but then I discovered that, if cardSession
> had *ever* been assigned a CardSession object with new, then the if
> statement executes the delete command, even if the object had since been
> deleted. Thus, I sometimes get an item doubly deleted with this code -
> an undefined behavior that I think is causing my program to crash. Note
> that if I have not yet used new in the program to assign an object to
> cardSession, the if statement does not execute.
>
> Could somebody explain this to me? Am I not understanding pointer usage
> correctly, or is it innapropriate usage of new/delete? How can I do
> this in an efficient fashion, without managing a global pointer to be
> modified everytime I create or delete an object?


Ok, I see what you're trying to do. delete is allowed to set the
pointer to null, but most implementations, like yours, don't do that.
So you have to explicitly write this as:

delete cardSession;
cardSession = 0;

This doesn't work if you do:

void foo(Card *c)
{
delete c;
c = 0;
}

Card *c = new Card();
foo(c);
// c points to garbage instead of null

You can rewrite that as:

void foo(Card *&c)
{
delete c;
c = 0;
}

and now it will correctly set the c that was passed to foo() to null.

 
Reply With Quote
 
Squid Seven
Guest
Posts: n/a
 
      07-07-2005


Chad J McQuinn wrote:
> Squid Seven wrote:
>
>> I create a pointer to an item:
>>
>> CardSession *cardSession;

>
>
> Change this to
>
> CardSession *cardSession = null;
>
> It's good practice to initialize the variable.
>
>> if( cardSession )
>> delete cardSession;
>>
>> I thought this would work, but then I discovered that, if cardSession
>> had *ever* been assigned a CardSession object with new, then the if
>> statement executes the delete command, even if the object had since
>> been deleted.

>
>
> Yes. Either you don't understand what if(cardSession) does, or you don't
> understand what delete does. For a pointer type such as the variable you
> have here, the conditional
>
> if(cardSession)
>
> is a check to see whether cardSession is a null pointer or not--it does
> NOT check if the pointed-to object is valid (and there is no way in
> general to perform such a check in standard C++).
>
> The subsequent delete statement destroys whatever is pointed to by
> cardSession. If a valid object isn't there, you are likely to crash.
>
> What you need to do here, and what is also good practice, is to add this
> after you delete:
>
> cardSession = null;
>
> Also be aware that your check for whether cardSession is null is not
> necessary. It is perfectly safe to delete a null pointer (but not an
> invalid one), so just
>
> delete cardSession;
> cardSession=null;
>
> in place of your original "if" will do the job.
>
> -cjm


When I try:

CardSession *cardSession = null;

I get the error message "'null' was not declared in this scope." Is
that a compiler-specific issue? Can I simply use:

CardSession *cardSession = 0;

as someone else suggested, and still not have to test before calling delete?

Thanks much for helping out a newbie guys.
 
Reply With Quote
 
Squid Seven
Guest
Posts: n/a
 
      07-07-2005


Squid Seven wrote:
>
>
> Chad J McQuinn wrote:
>
>> Squid Seven wrote:
>>
>>> I create a pointer to an item:
>>>
>>> CardSession *cardSession;

>>
>>
>>
>> Change this to
>>
>> CardSession *cardSession = null;
>>
>> It's good practice to initialize the variable.
>>
>>> if( cardSession )
>>> delete cardSession;
>>>
>>> I thought this would work, but then I discovered that, if cardSession
>>> had *ever* been assigned a CardSession object with new, then the if
>>> statement executes the delete command, even if the object had since
>>> been deleted.

>>
>>
>>
>> Yes. Either you don't understand what if(cardSession) does, or you
>> don't understand what delete does. For a pointer type such as the
>> variable you have here, the conditional
>>
>> if(cardSession)
>>
>> is a check to see whether cardSession is a null pointer or not--it
>> does NOT check if the pointed-to object is valid (and there is no way
>> in general to perform such a check in standard C++).
>>
>> The subsequent delete statement destroys whatever is pointed to by
>> cardSession. If a valid object isn't there, you are likely to crash.
>>
>> What you need to do here, and what is also good practice, is to add
>> this after you delete:
>>
>> cardSession = null;
>>
>> Also be aware that your check for whether cardSession is null is not
>> necessary. It is perfectly safe to delete a null pointer (but not an
>> invalid one), so just
>>
>> delete cardSession;
>> cardSession=null;
>>
>> in place of your original "if" will do the job.
>>
>> -cjm

>
>
> When I try:
>
> CardSession *cardSession = null;
>
> I get the error message "'null' was not declared in this scope." Is
> that a compiler-specific issue? Can I simply use:
>
> CardSession *cardSession = 0;
>
> as someone else suggested, and still not have to test before calling
> delete?
>
> Thanks much for helping out a newbie guys.


Oh, it was compiler-dependent. I need to use 'NULL', not 'null'.

All set now, thanks much.
 
Reply With Quote
 
Chad J McQuinn
Guest
Posts: n/a
 
      07-07-2005
Squid Seven wrote:

>
> CardSession *cardSession = null;
>
> I get the error message "'null' was not declared in this scope." Is
> that a compiler-specific issue? Can I simply use:
>
> CardSession *cardSession = 0;
>
> as someone else suggested, and still not have to test before calling
> delete?


Yes, sorry about that, it should be 'NULL' (not 'null') which should be
a #define for 0, so those two should be equivalent. This is in C++; in
plain C, as well as with some older compilers, you might see it #defined
to something else, such as (void*)0, which causes problems. For this
reason you'll see a lot of people use a literal zero as you just did. It
also avoids having to remember if it's NULL or null.

-cjm
 
Reply With Quote
 
Matthias Kluwe
Guest
Posts: n/a
 
      07-08-2005
> OK - let me see if I follow... When I create the pointer, if I don't
> initialize it to something, I don't know what it's pointing at - could
> be null, could not be. Then, when I delete the object associated with
> the pointer, while I am indicating that the memory pointed at is
> available for other use, the pointer still points at that memory, until
> assigned to something else, and so causes the if statement to execute
> it's contents?


Exactly. You cannot rely on the value of an unitialized variable
(pointer or not). You cannot rely on a pointer of some type pointing to
an object of this type (you have to care by yourself).

> Also, if this does not test if the pointer points to an object, how do I
> test for it?


As said in the thread, you can assing the value 0 (or NULL if you
really like to) to indicate no object is pointed to.

Alternatively, program logic should ensure that some function handling
the pointer is only called when this makes sense ...

Regards,
Matthias

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
test to see if a variable exists Colin Summers Ruby 4 06-16-2007 03:44 PM
Checking to see if a property exists within an object. Daz Javascript 3 05-04-2007 06:43 PM
finding if file exists or not in apache webdav server and if exists what is the mime type of it.... Totan Java 0 04-17-2006 05:13 AM
Test to see if variable exists lamar_air Python 3 08-12-2003 07:21 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments