Is the destructor always called for the delete operator, even if it is overloaded?

I am porting some old code from C to C ++. The old code uses object-like semantics and at some point separates the destruction of the object from the release of unused memory, and the material happens between them:

Object_Destructor(Object *me) { free(me->member1), free(me->member2) }

ObjectManager_FreeObject(ObjectManager *me, Object *obj) { free(obj) }

Is the above functionality possible in C ++ using a standard destructor ( ~Object) and subsequent call delete obj? Or, as I fear, can this cause the destructor twice?

In the particular case, operator deleteof is Objectalso redefined. Is the definition that I read elsewhere ("when the delete operator is used, and the object has a destructor, the destructor is always called) correctly in the redefined case of the operator?

+3
source share
9 answers

You can separate destruction from deletion, but you probably don't want this.

If you allocate memory with new char[]or malloc, and then call a new placement, you can separate the destruction (which you do by calling the destructor directly) from the delete (or free). But then you no longer call the overloaded class operator delete, instead you call the delete[]char (or free) array .

If you call delete with a pointer to your class (the one you overloaded with the delete operator for), this class destructor is called. Therefore, there is no way to separate them in the sense that you are asking about calling delete without a destructor.

+1

delete operator , , . , delete operator, .

, , ++ .

:

#include <iostream>
#include <new>
using namespace std;

struct foo {
    ~foo() { cout << "destructor\n"; }
    void operator delete(void* p) { 
        cout << "operator delete (not explicitly calling destructor)\n"; 
        free(p);
        cout << "After delete\n"; 
    }
};

int main()
{
    void *pv = malloc(sizeof(foo));
    foo* pf = new (pv) foo; // use placement new
    delete pf;
}

:

delete ( )

+6

delete - , , ( ). , "" , , , . , ( delete)

+3

? , , . , , .

, -, destruct. , , ++.

+2

, , .

(tm) .

class Clike
{
public:
  Clike() : m_usable(true) {}

  void clear(); // performs clean up then sets m_usable to false
  ~Clike() { if (m_usable) this->clear(); }

private:
  bool m_usable;
  // real variables
};

:

Clike* c = new Clike();

c->clear(); // performs cleanup

// stuff

delete c;

, , , cleanup destruction , cleanup . , DB Connections ..

"", , C- .....

+2

, .

delete .

- , , Stuff .

+1

std:: allocators . ", ". , .

0

, . , , . , .

-1

, , (, , , ).

- object_destructor() ( , ). , , , free() NULL , object_destructor , .

CLASS A {
   object_destructor() { free(this->member1); free(this->member2); }

   alternate_version_of_object_destructor() {  
           if (this->member1) { free(this->member1); this->member1= NULL; } 
           if (this->member2) { free(this->member2); this->member2= NULL; } }

    ~A() { /* do nothing or just call this->object_destructor() for safety */ }
}


foo() {
    A *pa= new A;


    pa->object_destructor();  /* member cleanup */
    delete pa;                /* free object memory */
} 
-1
source

Source: https://habr.com/ru/post/1723002/


All Articles