-1073741819 is hex 0xC0000005. That is the exit code for an uncaught Access Violation exception. Which means your code is accessing invalid memory.
Your vector is initialized with Test
objects that are holding null pointers. When you erase the 1st object, the remaining objects have to be moved down in the vector. But, since your class does not implement move semantics, the objects have to be copied instead. Your copy constructor is not handling the case when o.member
is nullptr
. And your class does not implement a copy assignment operator at all, thus violating the Rule of 3/5/0, which can lead to memory leaks and multiple objects ending up holding the same pointer causing double-deletes of memory.
Try this instead:
class Test {
int* member;
public:
Test() { member = new int(); }
Test(const Test& o) { member = new int(*(o.member)); }
~Test() { delete member; }
Test& operator=(const Test& rhs) {
if (&rhs != this) {
*member = *(rhs.member);
}
return *this;
}
};
Or, with move semantics added:
class Test {
int* member;
public:
Test() { member = new int(); }
Test(const Test& o) { member = new int(*(o.member)); }
Test(Test&& o) { member = o.member; o.member = nullptr; }
~Test() { delete member; }
Test& operator=(Test rhs) {
std::swap(member, rhs.member);
return *this;
}
};
That being said, consider using std::unique_ptr
to help with memory management:
class Test {
std::unique_ptr<int> member;
public:
Test() { member = std::make_unique<int>(); }
Test(const Test& o) { member = std::make_unique<int>(*(o.member)); }
Test(Test&& o) { member = std::move(o.member); }
Test& operator=(Test rhs) {
std::swap(member, rhs.member);
return *this;
}
};