When i use vector of class B, which contains allocated memory, double free error occurs.
class B
{
public:
std::string a;
std::string b;
int *hehe;
B()
{
a = "Hello";
b = ", World!";
hehe = new int[7];
for (int i = 0; i < 7; ++i) {
hehe[i] = i;
}
}
~B() {
if (hehe)
delete[] hehe;
}
};
std::vector<class B> a(5);
a.erase(a.begin() + 2);
Error message:
a.out(46830,0x10e0015c0) malloc: *** error for object 0x7ff12dc02a80: pointer being freed was not allocated a.out(46830,0x10e0015c0) malloc: *** set a breakpoint in malloc_error_break to debug
And this code is working fine. I am stunned.
std::vector<class B> a(1);
a.erase(a.begin());