Your problem is here:
Test tObj = Test();
Test() creates a temporary Test object, which is then copied to tObj . At this point, both tObj and the temporary object have big to point to the array. Then the temporary object is destroyed, which calls the destructor and destroys the array. Therefore, when tObj destroyed, it tries to destroy the already destroyed array again.
In addition, when tVec destroyed, it will destroy its elements, so the array already destroyed will be destroyed again.
You must define the copy constructor and assignment operator so that when you copy the Test object, the big array is copied or has some reference count, so that it is not destroyed until all the owners are destroyed.
A simple solution is to define your class as follows:
class Test { private: std::vector<int> big; public: Test (): big(10000) {} };
In this case, you will not need to define any destructor, copy constructor, or assignment operator, because the std::vector<> member will take care of everything. (But note that this means that 10,000 bytes are allocated and copied whenever you copy an instance of Test .)
Kristopher johnson
source share