I have a pretty basic class, "Cube", that has a COM pointer member. The default constructor initializes this pointer using the correct API functions, the copy constructor copies the pointer and calls AddRef(). The destructor calls Release(). An abridged version of the code:
[code=cpp]
class Cube {
public:
Cube(...) {
_x = CreateObj(...); //Obtains a (new) valid COM object pointer
}
Cube(const Cube &src) {
_x = src._x;
_x->AddRef();
}
~Cube() {
if (_x) {
_x->Release();
_x = 0;
}
}
protected:
IObj* _x; //A COM object
};[/code]
Next, I create a vector of these "Cubes" and allocate them using the function below (simplified):
[code=cpp]//Global
vector<Cube> arr;
void Init() {
Cube* cube;
for (int i=0; i<10; i++) {
cube = new Cube(...); //Obtains a (new) valid COM object pointer
arr.push_back(* cube);
}
}[/code]
Now, in my cleanup function, I iterate through the vector and delete the objects. However, as soon as I delete the first "Cube", all members of the vectors are deallocated (filled with 0xfeeefeee when running through the debugger). Code:
[code=cpp]void Cleanup() {
for (vector<Cube>:: iterator i = arr.begin(); i < arr.end(); i++) {
delete &(*i);
}
}[/code]
As soon as the delete line is called the second time my app crashes.
EDIT: FYI, all the objects are valid, working and unique during the execution (between the calls to Init() and Cleanup()).
[code=cpp]
class Cube {
public:
Cube(...) {
_x = CreateObj(...); //Obtains a (new) valid COM object pointer
}
Cube(const Cube &src) {
_x = src._x;
_x->AddRef();
}
~Cube() {
if (_x) {
_x->Release();
_x = 0;
}
}
protected:
IObj* _x; //A COM object
};[/code]
Next, I create a vector of these "Cubes" and allocate them using the function below (simplified):
[code=cpp]//Global
vector<Cube> arr;
void Init() {
Cube* cube;
for (int i=0; i<10; i++) {
cube = new Cube(...); //Obtains a (new) valid COM object pointer
arr.push_back(* cube);
}
}[/code]
Now, in my cleanup function, I iterate through the vector and delete the objects. However, as soon as I delete the first "Cube", all members of the vectors are deallocated (filled with 0xfeeefeee when running through the debugger). Code:
[code=cpp]void Cleanup() {
for (vector<Cube>:: iterator i = arr.begin(); i < arr.end(); i++) {
delete &(*i);
}
}[/code]
As soon as the delete line is called the second time my app crashes.
EDIT: FYI, all the objects are valid, working and unique during the execution (between the calls to Init() and Cleanup()).
Comment