Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting a heap pointer stored in a class in a vector 1

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo.
I have a class called Tfc_item, which contains a pointer to an object on the heap. When Tfc_item's destructor is called, this object is deleted.
My problem is, I am getting an exception class EPrivilege with message 'Privileged instruction'.
This doesn't mean a whole lot to me, but I have traced the problem to the point in the destructor when the object on the heap is deleted.
Does the STL's vector class also take care of deleting contained objects on the heap, or should I be looking for a problem elsewhere?
Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
EPrivilege is just a red herring. Is the item being deleted twice?

As for your second question, the answer is no.

vector<int*> xxx;

if you call

xxx.clear ();

The vector will be empty but the heap will still have lots of ints.
 
Yeah - I think a rather messy quickfix must be deleting the item twice - I guess I was just hoping against hope that I could find a quick answer.
Thanks alot, man.
DJL Common sense is what tells you the world is flat.
 
To delete a vector of pointers (eg vector<aClass*> myClass), try:

object::~object(void)
{
for(int x = 0; x < myClass.size(); x++)
delete myClass[x];

myClass.erase(myClass.begin(), myClass.end());
}

 
Hallo again - I'm getting the same exception, but really have no idea why.
I'm not using ANY pointers (except for those hidden in vector<T>), so it can't be any of my deletes.
Also, I can't even set breakpoints in the project's .cpp file - the exception occurs even before execution gets there.
Anybody any ideas?

Cheers,
Doug.

Common sense is what tells you the world is flat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top