abort and Destructors

From RAD Studio
Jump to: navigation, search

Go Up to Destructors Index

If you call abort anywhere in a program, no destructors are called, not even for variables with a global scope.

A destructor can also be invoked explicitly in one of two ways: indirectly through a call to delete, or directly by using the destructor fully qualified name. You can use delete to destroy objects that have been allocated using new. Explicit calls to the destructor are necessary only for objects allocated a specific address through calls to new"

#include <stdlib.h>

class X {
    // …
    // …

void* operator new(size_t size, void *ptr)
    return ptr;

char buffer[sizeof(X)];

void main()
    X* pointer = new X;
    X* exact_pointer;
    exact_pointer = new(&buffer) X; // pointer initialized at
                                        // address of buffer
    // …
    delete pointer;                     // delete used to destroy pointer
    exact_pointer->X::~X();          // direct call used to deallocate

See Also