STL: delete needed along with erase?

  • Thread starter AbrahamLincolnIllinois
  • Start date
A

AbrahamLincolnIllinois

Hi all.

I have a list of pointers to a complicated object. When I erase() a
member of that list, the little blob of memory that contains the
pointer is deleted, I think. But the object pointed to is not
deleted, true? It is not an auto_ptr or any kind of wrapped
pointer.

Further, I assume that the iterator I am holding is no longer valid
once I delete the element. I can't safely erase the member first,
then delete the object through the iterator. So the sequence ought to
be: 1. Delete the object. 2. Erase the list element.

I am checking my understanding, because the code I am working on,
which I inherited, doesn't delete the object at all, either before or
after the element is erased.


Abraham
 
V

Victor Bazarov

I have a list of pointers to a complicated object. When I erase() a
member of that list, the little blob of memory that contains the
pointer is deleted, I think.

Whatever the implementation does to get rid of it, yes.
But the object pointed to is not
deleted, true?
True.

It is not an auto_ptr or any kind of wrapped
pointer.

Right. You cannot use 'auto_ptr', but 'shared_ptr' should be fine.
Further, I assume that the iterator I am holding is no longer valid
once I delete the element.

You mean, once you *erase* it? "Delete" has a particular meaning in
C++, as you know. Yes, the iterator is invalid if you erase the list
element behind it.
I can't safely erase the member first,
then delete the object through the iterator. So the sequence ought to
be: 1. Delete the object. 2. Erase the list element.

That sounds right.
I am checking my understanding, because the code I am working on,
which I inherited, doesn't delete the object at all, either before or
after the element is erased.

Good thinking.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top