我的存儲器中的以下程序的雙重釋放的問題。
調試器顯示問題出在push_back()
函數中。
類A:B
class A {
public:
A(int x);
int x;
};
A::A(int x) {
this->x = x;
}
類別:
class B {
public:
B(int x);
~B();
A* a;
};
B::B(int x) {
this->a = new A(x);
}
B::~B() {
delete a;
}
主要功能:
int main() {
vector<B> vec;
for(int i = 0; i < 10; i++) {
vec.push_back(B(i)); <------------ Issue is here
}
cout << "adding complete" << endl;
for(int i = 0; i < 10; i++) {
cout << "x = " << (vec[i].a)->x << endl;
}
return 0;
}
什麼是錯誤的第是代碼?
編輯:錯誤double free or memory corruption
你看到了什麼錯誤? –
http://stackoverflow.com/questions/4172722/what-is-the-rule-of-reeree –
請參閱http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – hmjd