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

Created on 2022-08-27.00:00:00 last changed 16 months ago

Messages

Date: 2022-10-07.20:29:56

Proposed resolution (approved by CWG 2022-10-07):

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-11-15.00:00:00

[Accepted as a DR at the November, 2022 meeting.]

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
2023-07-16 13:00:43adminsetstatus: open -> c++23
2023-07-16 13:00:43adminsetstatus: drwp -> open
2023-02-18 18:43:04adminsetstatus: dr -> drwp
2022-11-25 05:14:04adminsetstatus: ready -> dr
2022-10-07 20:29:56adminsetstatus: open -> ready
2022-09-06 07:05:17adminsetmessages: + msg6929
2022-08-27 00:00:00admincreate