Why should I not try to use “this” value after “delete this”?

The value of ‘this’ after calling delete is undefined, and the behaviour of anything you do with it is also undefined. While I would expect most compilers to do something sensible, there’s nothing (in the spec) stopping the compiler from deciding that its behaviour in this particular case will be emit code to format your hard-disk. Invoking undefined behaviour is (almost) always a mistake, even when your particular compiler behaves in the way you’d like it to.

You could work around this by taking a copy of the pointer (as an integer) before calling delete.

Leave a Comment