Is it good practice to call a function inside a destructor in C ++

Is it good practice to call a function inside the destructor that does some memory allocation internally. Because it gives me access violations and other problems. Guess

~Example(){ Stop(); } 

Here in this function Stop () performs various actions, and also calls other functions? This is a good practice. Can anyone help with this?

+4
source share
6 answers

There is nothing wrong with calling functions inside the destructor, the only important thing to consider is that there should be no uncaught exceptions emanating from the destructor.

So, until you catch all the exceptions caused by functions called inside the destructor inside the descriptor, you are safe.

+8
source

Calling the method from the destructor in order as long as it is part of the release / release process. It is not recommended to perform the operation in the opposite direction (obtaining resources / memory).

0
source

Destructors are called (among others) when std::bad_alloc , i.e. at memory failure. In this case, the memory allocation in the destructor (or the function called by the destructor) is also likely to fail, throwing an exception.

Throwing exceptions from the destructor is a really bad idea. Now the destructor can catch the second second std::bad_alloc , but I doubt that it has the necessary tools to solve this problem.

0
source

As others have said, this is normal, but can be dangerous. Keep in mind that some parts of the class may have already been destroyed, so you may not be able to use everything in your class. Also, try to handle exceptions using the method.

0
source

What the destructor has to do directly follows from the state of the class and design. Thus, the choice is subtle. You can do this in a strict expression or call a function. I see no reason why not to call a function, especially if other member functions can also use it. (See Implementing smart pointers having a reset() function ...)

You say that if the work is difficult, we better not do it. Hmmm. Or does the idea just not do it in dtor, but leave this obligation to the programmer? How to return to C? Assuming that the most attractive feature of C ++ has dtors that allow RAII?

The only thing to do in dtor is to grasp the exceptions. With some luck, they naturally cannot happen. If there is a chance, it’s really nice to have some kind of social function to carry out most of the tasks from which to quit - an honest game. (Ie CFile class closes the file in dtor if it is still open. When used for the output file, you must close it manually and deal with possible errors, such as the inability to flush the last buffers to a full disk. All this and dtor will be freed from hard work .

0
source

Nothing wrong with the call function in the destructor, considering the following sineers

a) If the function is not virtual

http://www.artima.com/cppsource/nevercall.html

b) Avoid calling a member function that refers to those that have already been destroyed

 bob::~bob() { delete ptr; this->increment(p); // undefined behavior } 

c) Prevention of destructor execution.

If the destructor is called due to an exception, the application is already terminated.

0
source

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


All Articles