简体   繁体   English

如何正确定义析构函数

[英]How to properly define destructor

I am relatively new to C++ (and programming in general) so please forgive me if the question is not perfectly clear immediately. 我对C ++(以及一般的编程)比较陌生,所以如果问题不是很清楚,请原谅我。

What I have is a program in which a certain number of objects of a internally defined class [let's call this "class1"] are created. 我所拥有的是一个程序,其中创建了一个内部定义的类[我们称之为“class1”]的一定数量的对象。 The program works perfectly fine and the objects do what they should. 该程序完美无缺,对象可以完成它们应有的功能。

The problem that I am currently trying to solve is the following: these objects are not destroyed (and thus memory is not de-allocated) until the program exits, but I need that memory earlier. 我目前要解决的问题如下:在程序退出之前,这些对象不会被销毁(因此内存不会被解除分配),但我需要更早的内存。

Among the other members of the class there are objects of other internally defined classes (who also have members that are objects of a third class). 在该类的其他成员中,存在其他内部定义的类的对象(其还具有作为第三类的对象的成员)。

My question is the following: how do I properly define a destructor for the objects of "class1" so that all the data is cancelled and the memory deallocated? 我的问题如下:如何正确定义“class1”对象的析构函数,以便取消所有数据并释放内存?

I found out (probably this was obvious for you already) that a destructor like 我发现了(可能这对你来说很明显)一个析构函数就像

class1::~class1(void) {

}

won't work (I defined similar destructors for all internally defined classes). 将无法工作(我为所有内部定义的类定义了类似的析构函数)。

Reading around I understood that my mistake could be that that is a destructor that does nothing. 阅读我明白我的错误可能是那个什么都不做的析构函数。 How do I solve this? 我该如何解决这个问题?

Thanks to anyone who will answer/help/comment. 感谢任何回答/帮助/评论的人。

Federico 费德里科

In C++ you need to free the memory manually. 在C ++中,您需要手动释放内存。 There's no garbage collector. 没有垃圾收集器。 You obviously need to free the memory manually inside your destructor. 你显然需要在析构函数中手动释放内存。 If you allocated the memory using new , you need to use delete for each resource you allocated with new inside the deconstructor, for example: 如果您在使用分配的内存,你需要使用删除你的解构与内部new分配的每个资源,例如:

class1::~class1(void)
{
    delete resource1;
    delete resource2;
    etc...
}

If you are allocating memory dynamically you need to free it in destructor, but better solution would be to use some smart pointer to hold dynamic data - std::auto_ptr or std::shared_ptr . 如果你动态分配内存,你需要在析构函数中释放它,但更好的解决方案是使用一些智能指针来保存动态数据 - std::auto_ptrstd::shared_ptr Then you will not need to explicitly free memory - this will be done automatically in smart pointer destructor. 然后你不需要显式释放内存 - 这将在智能指针析构函数中自动完成。

Memory on stack 堆栈上的内存

If there is no heap-allocated object inside your class, there is no need for you to explicitly define the destructor of your class. 如果类中没有堆分配的对象,则无需显式定义类的析构函数。 The compiler-generated one should handle the job quite well, ie, call destructors for base class objects and member objects, etc. 编译器生成的应该很好地处理这个工作,即调用基类对象和成员对象的析构函数等。

Memory on heap - manual memory management 堆上的内存 - 手动内存管理

If there is any heap-allocated objects inside your class, you need manually deallocate them in your destructor. 如果类中有任何堆分配的对象,则需要在析构函数中手动取消分配它们。 Make sure you have them deallocated properly at all possible exit points , eg, handle exceptions and deallocate resources. 确保在所有可能的出口点正确释放它们,例如,处理异常和释放资源。

Memory on heap - RAII 堆上的内存 - RAII

Alternatively, you may use some well-defined RAII class to handle the resource management for you automatically, eg, scoped_ptr , scoped_array in Boost, shared_ptr in STL, etc. 或者,您可以使用一些定义良好的RAII类来自动处理资源管理,例如scoped_ptr ,Boost中的scoped_array ,STL中的shared_ptr等。

First you should try to allocate your objects on the stack: 首先,您应该尝试在堆栈上分配对象:

Object obj;

so you don't have to worry about memory allocation. 所以你不必担心内存分配。 The disadvantage is that the object life is limited to the current scope. 缺点是物体寿命限于当前范围。

If that's not good for you, you should consider using smart pointers. 如果这对你不好,你应该考虑使用智能指针。 If that's not an option use new / delete to create / destroy your objects. 如果这不是一个选项,请使用new / delete来创建/销毁对象。 But you have to be careful, every new should lead to a delete at some point in time.. 但是你必须要小心,每一个new应该导致在某个时间点delete ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM