Title
Deletion of pointer to out-of-lifetime object
Status
open
Section
6.7.3 [basic.life]
Submitter
Blacktea Hamburger

Created on 2022-08-27.00:00:00 last changed 3 weeks ago

Messages

Date: 2022-09-06.07:05:17

Suggested resolution:

Change in 6.7.3 [basic.life] bullet 6.1 as follows:

The program has undefined behavior if:
  • the object will be or was of a class type with a non-trivial destructor and the pointer is used as the operand of a delete-expression,
  • ...
Date: 2022-08-27.00:00:00

Consider:

struct S {};

int main() {
  S* p = new S;
  p->~S();
  delete p;
}

This code appears to be allowed per 6.7.3 [basic.life] bullet 6.1:

The program has undefined behavior if:
  • the object will be or was of a class type with a non-trivial destructor and the pointer is used as the operand of a delete-expression,
  • ...

However, this calls the (trivial) destructor on *p twice. Invoking a non-static member function of an out-of-lifetime object is generally undefined behavior per 6.7.3 [basic.life] bullet 6.2 and 6.7.3 [basic.life] bullet 7.2. The rules ought to be consistent.

History
Date User Action Args
2022-09-06 07:05:17adminsetmessages: + msg6929
2022-08-27 00:00:00admincreate