Boost logo

Boost :

From: Mark Rodgers (mark.rodgers_at_[hidden])
Date: 2002-02-03 15:38:49

From: "Rainer Deyke" <root_at_[hidden]>
> It sounds like your 'auto_vector' avoids some of the problems of a
> container of auto_ptrs by conceptually being a container of vanilla
> pointers. Unfortunately this has its own set of problems. Consider:
> auto_vector<Cat> cats;
> cats.push_back(new Cat);
> cats[0] = new Cat; // Error: memory leak

With my current version, yes this is unfortunately true. I know
enough not to do that, and so it has never been a problem for me.
I regard auto_vector as being convenient and not full proof.

If more safety is required, I guess we could look at making
operator[], front, and back return values rather than references.
But it would still cause a leak if you did a

  *cats.begin() = new Cat;

We could then make all iterators const, but that would stop us
sorting the vector. Then I guess we could add a sort member

Or I guess we could go the whole hog and return some sort of
proxy that deleted the current pointer on assignment.

But I don't know if the added safety of either is worth the
trouble. What do others think? Keep it simple or make it safe?


Boost list run by bdawes at, gregod at, cpdaniel at, john at