Title
Is deallocation routine called if destructor throws exception in delete?
Status
cd1
Section
7.6.2.9 [expr.delete]
Submitter
Duane Smith

Created on 2002-04-30.00:00:00 last changed 189 months ago

Messages

Date: 2003-04-15.00:00:00

[Voted into WP at April 2003 meeting.]

Date: 2003-04-15.00:00:00

[Voted into WP at April 2003 meeting.]

Date: 2022-11-20.07:54:16

In a couple of comp.std.c++ threads, people have asked whether the Standard guarantees that the deallocation function will be called in a delete-expression if the destructor throws an exception. Most/all people have expressed the opinion that the deallocation function must be called in this case, although no one has been able to cite wording in the Standard supporting that view.

#include <new.h>
#include <stdio.h>
#include <stdlib.h>

static int flag = 0;

inline
void operator delete(void* p) throw()
{
   if (flag)
        printf("in deallocation function\n");
   free(p);
}

struct S {
    ~S() { throw 0; }
};

void f() {
    try {
        delete new S;
    }
    catch(...) { }
}

int main() {
       flag=1;
       f();
       flag=0;
       return 0;
}

Proposed resolution (October 2002):

Add to 7.6.2.9 [expr.delete] paragraph 7 the highlighted text:

The delete-expression will call a deallocation function (6.7.5.5.3 [basic.stc.dynamic.deallocation]) [Note: The deallocation function is called regardless of whether the destructor for the object or some element of the array throws an exception. ]
History
Date User Action Args
2008-10-05 00:00:00adminsetstatus: wp -> cd1
2003-04-25 00:00:00adminsetmessages: + msg860
2003-04-25 00:00:00adminsetstatus: ready -> wp
2002-11-08 00:00:00adminsetmessages: + msg729
2002-11-08 00:00:00adminsetstatus: open -> ready
2002-04-30 00:00:00admincreate