简体   繁体   English

未分配被释放的指针 - 析构函数

[英]Pointer being freed was not allocated - destructor

Slowly learning about copy/move constructors, rule of 5 etc. Mixed with not-so-well understanding of usage of pointers/reference, I cannot understand why my destructor throws the error below.慢慢了解复制/移动构造函数、5 规则等。再加上对指针/引用的使用不太了解,我无法理解为什么我的析构函数会抛出下面的错误。 I know that it's caused by destructor and destructor only, and after the copy constructor.我知道它仅由析构函数和析构函数引起,并且在复制构造函数之后。

untitled(19615,0x104a60580) malloc: *** error for object 0x600000b74000: pointer being freed was not allocated

untitled(19615,0x104a60580) malloc: *** set a breakpoint in malloc_error_break to debug

Code:代码:

#include <string>

using namespace std;
typedef int size_type;

class user{
public:
    user():
    rank(1),
    name("default"){
    }
private:
    int rank;
    string name;
};

class account{
public:
    account(size_type max_size):
    owners(new user[max_size]),
    max_size(max_size){
        cout<<"normal constructor"<<endl;
    }
    account(account& other):
    owners(other.owners),
    max_size(sizeof(owners)){
        cout<<"copy constructor called"<<endl;
    }
    account(account&& other):
    owners(other.owners),
    max_size(sizeof(owners)){
        cout<<"move constructor called"<<endl;
    }
    ~account(){
        if(owners!= NULL) {
            delete[] owners;
            owners= 0;
        }
        cout<<"destructor called"<<endl;
    }

private:
    user* owners;
    size_type max_size;
};

int main(){
    account f(3);
    account n(f);
}

I cannot understand why my destructor throws the error below.我不明白为什么我的析构函数会抛出下面的错误。 I know that it's caused by destructor and destructor only我知道它是由析构函数和析构函数引起的

account f(3);

The constructor of this object allocates an array.这个 object 的构造函数分配了一个数组。

 account n(f);

The copy constructor that you wrote copies the pointer that points to the dynamic array.您编写的复制构造函数复制指向动态数组的指针。

n is destroyed first. n首先被销毁。 Its destructor deletes the array.它的析构函数删除了数组。 f is destroyed after that.之后f被销毁。 Its destructor also deletes the same array and the behaviour of the program is undefined, which lead to the output that you observed.它的析构函数也删除了相同的数组并且程序的行为是未定义的,这导致了您观察到的 output。

To prevent the allocation from being deleted multiple times, you must enforce a class invariant (a post condition that applies to all member functions) that any non-null owners value must be unique across all instances of the class.为防止分配被多次删除,您必须强制执行 class 不变量(适用于所有成员函数的后置条件),即任何非空owners值在 class 的所有实例中必须是唯一的。 To maintain that invariant in the copy constructor (as well as move constructor and copy and move assignment operators), you must not copy the pointer, as you do now.要在复制构造函数(以及移动构造函数以及复制和移动赋值运算符)中保持该不变性,您不能像现在那样复制指针。 I recommend studying the RAII idiom.我建议学习 RAII 成语。

Better solution: Don't use owning bare pointers.更好的解决方案:不要使用拥有裸指针。 Use std::vector<user> .使用std::vector<user>

PS The copy constructor should accept a reference to const. PS 复制构造函数应该接受对 const 的引用。

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

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