Your problem is here:
Test tObj = Test();
The Test()
creates a temporary Test
object, which then gets copied to tObj
. At this point, both tObj
and the temporary object have big
set to point to the array. Then the temporary object gets destroyed, which calls the destructor and destroys the array. So when tObj
gets destroyed, it tries to destroy the already-destroyed array again.
Further, when tVec
is destroyed, it will destroy its elements, so the already-destroyed array will be destroyed yet again.
You should define a copy constructor and an assignment operator so that when a Test
object gets copied, the big
array gets copied, or has some sort of reference count so that it doesn't get destroyed until all owners are destroyed.
An easy fix is to define your class like this:
class Test
{
private:
std::vector<int> big;
public:
Test (): big(10000) {}
};
In this case, you wouldn't 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 10,000 bytes get allocated and copied whenever you copy an instance of Test
.)