J
Joe Van Dyk
When you delete a pointer, you should set it to NULL, right?
Joe
Joe
Joe said:When you delete a pointer, you should set it to NULL, right?
Joe said:When you delete a pointer, you should set it to NULL, right?
Phlip said:No. Your pointer should always be about to go out of scope, so don't
bother to NULL it.
If you must, use std::auto_ptr or boost::shared_ptr, and reset(NULL)
that (if I recall correctly).
[..]
No. If you delete a pointer, it's probably a class member, and you're
deleting it from a destructor.
In that case, you don't set it to 0,
because
noone will ever notice. The other common case is in assignments, [..]
Victor said:And if he's not?
Joe said:I'm not, actually.
Let's see if I can explain what's going on...
I've got an object (Object A) that contains two pointers to objects that
Object A didn't create.
There's another object in the system, Object B. Object B has a function
that requires an Object A pointer.
Object B gets deleted. It is (and out of my control) deleting the
Object A pointer. It's not setting the Object A pointer to NULL.
Now, there's threads involved here, so that may be complicating
things... but I was curious as to if the Object A pointer should be set
to NULL when Object B deletes it.
<snip>
Joe
C. J. Clegg said:You don't HAVE to.
doesn't hurt and is often helpful.
Joe said:[..]
Let's see if I can explain what's going on...
I've got an object (Object A) that contains two pointers to objects
that Object A didn't create.
There's another object in the system, Object B. Object B has a
function that requires an Object A pointer.
Object B gets deleted. It is (and out of my control) deleting the
Object A pointer.
It's not setting the Object A pointer to NULL.
Now, there's threads involved here, so that may be complicating
things... but I was curious as to if the Object A pointer should be
set to NULL when Object B deletes it.
Joe said:Object B gets deleted. It is (and out of my control) deleting the
Object A pointer. It's not setting the Object A pointer to NULL.
Joe said:I'm not terribly smart, you see.
Phlip said:Joe Van Dyk wrote:
Why aren't you using a smart pointer? Then they can do the thinking for you.
(Roughly..
private:Victor said:Joe said:[..]
Let's see if I can explain what's going on...
I've got an object (Object A) that contains two pointers to objects
that Object A didn't create.
So something like
class Other;
class A {
Other *not_mine_1, *not_mine_2;
public:
~A() {} // do nothing to pointers
};
There's another object in the system, Object B. Object B has a
function that requires an Object A pointer.
class B {
public:
void foo(A* aptr);
};
Which A pointer? You didn't say that B _owned_ a dynamic A object.
Shouldn't be a problem _even_if_ the B actually owned that A and had
a pointer to that dynamic object.
Well, no. Your problem is most likely is elsewhere. Read about "The
Rule of Three" and follow it (concerning the B class). Also, make sure
you're not deleting that B object _twice_ somewhere.
V
Joe Van Dyk said:When you delete a pointer, you should set it to NULL, right?
Joe
andrew said:I'm a little surprised at the responses you've been getting from some very
experienced programmers.
I think the correct answer to your question should be: if you intend to
check the value of the pointer later on you should set it to NULL
Phlip said:Another alternative is to set the pointer equal to the address of a static
NullObject. Consider this code:
p = getPointerFromWhatever();
if (p)
p->method(42);
Using NullObjects, that becomes this:
p = getPointerFromWhatever();
assert(p);
p->method(42);
The code is one tick simpler; it has one less 'if' statement.
Noah said:Really? What is that assert doing there then?
What type does this static NullObject have?
Phlip said:It is not increasing the mental burden of reading the function. And we could
migrate it inside a new function, getReferenceFromWhatever(), and this
function would be even shorter.
A type derived from Whatever class type that p points to, with
Whatever::method(int) overriden as a no-op.
That requires three things:
1) a special "NullObject" class for each and every class that could
have a pointer to it.
2) a special "NullObject" static for each of the above implementations.
3) that all classes and all member functions are polymorphic!!
Consider also functions that return values...they cannot be noops!
Now #2 is only by consequence of your initial requirements...it isn't
actually required to be static but could simply be constructed. This
results in more small objects than a static though.
#3 is the real killer.
I don't think you have thought this through quite enough yet though.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.