1

I have a class A

class A
{
  A();
  ~A();

  DoSomething();
}

I then declare an object and a pointer to the allocated object

A Obj;
A* pObj = &Obj;

I've found out that inside DoSomething() I am setting pObj to NULL

A::DoSomething()
{ 
    pObj=NULL;
}

So inside a method I am killing the pointer to myself... and this isn't crashing...

note: I've noticed that in the debbuger this is (Obj const).

I am having some trouble understanding why this isn't causing any kind of corruption. I assume that this isn't safe, but I can't really understand the ramifications of this.

Cheers

0

4 Answers 4

6

So inside a method I am killing the pointer to myself... and this isn't crashing...

There's absolutely no reason why it would crash. You can have many pointers to an object. The fact they you're resetting one or more of them to NULL doesn't automatically mean that anything bad should happen.

Two types of problems to be aware of are:

  1. if you try to dereference a pointer after setting it to NULL, the behaviour of your code will be undefined (most likely it'll crash).
  2. if the object is allocated on the heap (yours isn't) and you reset all pointers to it without calling delete, you'd leak memory.
Sign up to request clarification or add additional context in comments.

Comments

2

Consider pObj to be a piece of paper with an address written on it. If you burn this piece of paper, the building it was "pointing to" will remain intact. Removing all references and pointers to object does not free it automatically.

By the way, you can even call delete this; inside class's method and it will also work unless you'll try to access members of that instance after deleting. However, this solution should be used only when really needed.

Remember though, that if it is the only pointer to the instance, you will no longer have access to it and it will result in memory leak.

Comments

1

Your object remains untouched. Merely some pointer to the object is changed. That’s not a problem. Here’s a visualisation:

+---------+
|   Obj   |  <-------- pObj
+---------+

Now all you are doing inside A::DoSomething is removing the arrow from the picture above. Obj goes on as if nothing happened.

Comments

0

It isn't crashing, because from inside the instance access doesn't happen through the pointer stored in the variable pObj, but an internal, implicit constant pointer, that can be accessed by the this keyword (not that this is not that variable, it's just a way to get a pointer to where the instance is actually located in memory, but it behaves as a constant pointer for any practical application).

So when you overwrite the variable pObj with NULL, your object doesn't go away, and all internal references within the object are still valid, as those don't rely on any variables you define, but are resolved by the compiler to directly refer to the instances position in memory.

2 Comments

"this is not that variable ... but it behaves as a constant pointer" -- because it's actually an rvalue expression with pointer type. So it is a pointer value, but like you say it's not a variable. Just like static_cast<A*>(0) is an rvalue expression with pointer type, although a different value from this.
@SteveJessop: Indeed it is. But I was assuming that the OP doesn't know what "rvalues" are, so I tried to explain it in simplified terms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.