H
He Shiming
Hi,
I've developed a class that implements an interface definition. It looks
like this:
class IRecord {
public:
// define interface methods by pure virtual methods
// no member variables
}
class CRecord : public IRecord {
public:
// implement interface methods
}
Now, I'm creating a collection of the interface pointers using:
std::vector<IRecord*> vRecPtrs; The following code is used to create a
series of interface pointers and push them into the vector:
std::vector<IRecord*> vRecPtrs;
vRecPtrs.resize (vRecPtrs.size()+1);
vRecPtrs[vRecPtrs.size()-1] = (IRecord*) new CRecord();
// well, I do have to resize the vector to get a new position before I can
create the new interface instance in my actual case. I can't use
vector::insert, nor vector:ush_back.
So I pushed 100 instances to the vector. I iterate through all of them,
calling their methods, and they turned out fine.
However, something will screw up after I call vector::erase. In a case, I
have to remove an item from the collection, say:
delete vRecPtrs[10];
vRecPtrs.erase(vRecPtrs.begin()+10);
The 10th instance will be removed successfully. But the last item
(vRecPtrs.size()-1) will no longer be a valid pointer. Accessing any methods
of the last item will result in an access violation reading crash. I checked
the the pointer value of the last item through debuggers, and it does look
like a real pointer. Its value is expected, and does look like others. But
why can't I use it anymore? Why is the last item? Should I use some "safer
vector" for pointers?
Thanks
I've developed a class that implements an interface definition. It looks
like this:
class IRecord {
public:
// define interface methods by pure virtual methods
// no member variables
}
class CRecord : public IRecord {
public:
// implement interface methods
}
Now, I'm creating a collection of the interface pointers using:
std::vector<IRecord*> vRecPtrs; The following code is used to create a
series of interface pointers and push them into the vector:
std::vector<IRecord*> vRecPtrs;
vRecPtrs.resize (vRecPtrs.size()+1);
vRecPtrs[vRecPtrs.size()-1] = (IRecord*) new CRecord();
// well, I do have to resize the vector to get a new position before I can
create the new interface instance in my actual case. I can't use
vector::insert, nor vector:ush_back.
So I pushed 100 instances to the vector. I iterate through all of them,
calling their methods, and they turned out fine.
However, something will screw up after I call vector::erase. In a case, I
have to remove an item from the collection, say:
delete vRecPtrs[10];
vRecPtrs.erase(vRecPtrs.begin()+10);
The 10th instance will be removed successfully. But the last item
(vRecPtrs.size()-1) will no longer be a valid pointer. Accessing any methods
of the last item will result in an access violation reading crash. I checked
the the pointer value of the last item through debuggers, and it does look
like a real pointer. Its value is expected, and does look like others. But
why can't I use it anymore? Why is the last item? Should I use some "safer
vector" for pointers?
Thanks