B
Brian
Dear Programmers,
I have a class with a pointer to an array. In the destructor, I just
freed this pointer. A problem happens if I define a reference to a
vector of this kind of class. The destruction of the assigned memory
seems to call the class destructor more than once. I don't know the
reason or whether I used the vector class correctly. Attached is my
program. Thanks for your help.
Regards,
Brian
Run Result:
$ ./a.out
done
*** glibc detected *** double free or corruption (top): 0x08fa40a0 ***
Aborted
Code:
#include <iostream>
#include <vector>
using namespace std;
class ArrayWrap
{
public:
char * pt_ch;
ArrayWrap()
{
pt_ch = new char[100];
}
~ArrayWrap() {
delete[] pt_ch; // if delete this statement, no error then,
// but not a reasonable design
}
};
int main()
{
ArrayWrap dum;
ArrayWrap & rdum = dum;
vector<ArrayWrap> dummy(10);
vector<ArrayWrap> & refdummy = dummy;
cout << "done" << endl;
return 0;
}
I have a class with a pointer to an array. In the destructor, I just
freed this pointer. A problem happens if I define a reference to a
vector of this kind of class. The destruction of the assigned memory
seems to call the class destructor more than once. I don't know the
reason or whether I used the vector class correctly. Attached is my
program. Thanks for your help.
Regards,
Brian
Run Result:
$ ./a.out
done
*** glibc detected *** double free or corruption (top): 0x08fa40a0 ***
Aborted
Code:
#include <iostream>
#include <vector>
using namespace std;
class ArrayWrap
{
public:
char * pt_ch;
ArrayWrap()
{
pt_ch = new char[100];
}
~ArrayWrap() {
delete[] pt_ch; // if delete this statement, no error then,
// but not a reasonable design
}
};
int main()
{
ArrayWrap dum;
ArrayWrap & rdum = dum;
vector<ArrayWrap> dummy(10);
vector<ArrayWrap> & refdummy = dummy;
cout << "done" << endl;
return 0;
}