Class destructor memory management in C ++

What potential memory leaks will not have a hidden descriptor? I know that if you have anything stored on the heap, it will not process it, and if you have a connection to a file or database, this must be handled manually. Is there anything else? Like, say, non-basic data types like vectors?

Also, in an explicit destructor, do you need to destroy heapless variables that would be destroyed implicitly or would they be processed automatically?

thank

+3
source share
7 answers

What potential memory leaks will not have a hidden descriptor? I know that if you have anything stored on the heap, it will not process it, and if you have a connection to a file or database, this must be handled manually. Is there anything else? Like, say, non-basic data types like vectors?

, . , , - , , .

; , . , . , : int, float, double, short, bool .., .

, , ?

, , .

:

  • - .

:

  • - .

, , , , , .

, , RAII ( - ). , , , , , . , , . boost:: shared_ptr. (btw, , ++, ...)

+1

.. , .

-, ( - ), .. , , . , , . RAII :)

+4

, . , : , .

, , - .

, " " - , . , .

, " " FILE *, , FILE * - C-, . FILE * . " " std::ofstream, , .

, . , .

-, , , , . , .

- , . , , , , .

, ++, , , , . : , , , , , -:). , - - -. , , , . shared_ptr .

" "

++ "" " ". , , , , , , "".

+4

. , t t .

class T
{
    A a;
    B b;
    C c;

    // ...

} t;
  • t : ~T(), ~C(), ~B(), ~A(). . . , , ~T() .

  • . , 1, ~C(), ~B() ~A() - .

  • , C, . , ( ) , - C - ~T(), C , , , ~T().

, .

+3

-.

, . , , , .

, , -.

- , , auto_ptrs, , std- ++, . , , ++, , .

+1

, , .

STL , - .

0

. , , std::auto_ptr<Data>, dtor . . , implict dtor, dtor, dtors .

Now a good class class contains all the important resources as members with corresponding dtors. As a result, the dtor body does not have to do anything to prevent resource leakage. Of course, there is one exception to this rule: the resource management classes themselves manage exactly one resource and, therefore, clear this one resource from its dtor.

0
source

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


All Articles