#include <iostream>
#include <memory>
using namespace std;
class Boom{
private:
int no;
public:
Boom():no(10){
cout << "Create Boom 1..." << endl;
}
Boom(int a):no(a)
{
cout << "Create Boom 2..." << endl;
};
~Boom(){
cout << "Destructing.." << " " << no << endl;
};
};
class BoomPoiner {
private:
Boom* obj;
public:
BoomPoiner(){
cout << "Create pointer..." << endl;
}
BoomPoiner operator=(Boom *obj){
this->obj = obj;
return *this;
}
~BoomPoiner(){
cout << "Delete pointer obj..." << endl;
delete obj;
}
};
void makePointer(){
//std::unique_ptr<Boom> boom = std::make_unique<Boom>(1);
//Boom* boom2 = new Boom(2);
Boom* boom = new Boom();
BoomPoiner ptr;
ptr = boom;
cout << "out func" << endl;
return;
}
int main(){
makePointer();
return 0;
}
When I tried to do something like above, it will keep call the destructor of BoomPointer. I only have created one BoomPointer instance and one Boom instance. Theoretically, it should call only once for the destructor.
The result:
Create Boom 1…
Create pointer…
Delete pointer obj…
Destructing.. 10
out func
Delete pointer obj…
Destructing.. 0
main(59838,0x1eaf39b40) malloc: Double free of object 0x100204120
main(59838,0x1eaf39b40) malloc: *** set a breakpoint in
malloc_error_break to debug
2