Re: memory leak in <vector> STL

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: SaltPeter (SaltPeter_at_Jupiter.sys)
Date: 07/20/04


Date: Tue, 20 Jul 2004 09:58:16 -0400


"Gerd Keller" <Gerd.Keller@smail.inf.fh-bonn-rhein-sieg.de> wrote in message
news:#NNOkyjbEHA.3716@TK2MSFTNGP11.phx.gbl...
> Hello,
>
> I am using a vector of pointers in a dynamic fashion.
> - At the end of my function i add dynamically pointers to the vector
> - Process data somewhere else
> - next time i call the function i clear the vector
> first release the pointer then memory
>
> <code>
> void OSTRoundFeature::findFeatures( IplImage *input_img )
> {
> ...
> //clear and deallocate memory
> //first clear pointer then memory
> vector< OSTFeatureModel* >::iterator run = vectorFeatureModel.begin();
> while( run != vectorFeatureModel.end() )
> {
> OSTFeatureModel* del = *run;
> run = vectorFeatureModel.erase( run );
> delete del;
> }

Why are you erasing an iterator? A vector is not meant to be used like like
a coin collector where a coin falls down when the bottom one is removed. Why
aren't you incrementing run instead to prepare your next iteration through
the loop? You need not erase the iterators. Doing so displaces the whole
container needlessly every time around the loop.

try:
vector<OSTFeaturemodel*>::iterator run;
for(run = vectorFeatureModel.begin(); run != vectorFeatureModel.end();
++run)
{
    delete *run;
}

Your way keeps copying the whole container around each loop since erasing
the first element iterator requires a copy of all the iterators above it
(you'll see the copy while debugging single-step). Imagine doing this with a
data set of a few million entries...

> ...
> //create object (allocate memory)
> //add pointer to vector
> OSTFeatureModel *tempFeature = new OSTFeatureModel();
>
> tempFeature->position->x = boundingBox.x+(bounding,Box.width/2);
> tempFeature->position->y = boundingBox.y+(boundingBox.height/2);
>
> tempFeature->box->northWestX = boundingBox.x;

wow, all of a sudden, the OSFFeatureModel abstract type has pointers within.
But we need not see the class since its obvious that you'll properly code
better than the whole ANSI/ISO standard committee members that have been
working on refining the C++ standard and STL containers for about 2 decades
now.

> tempFeature->box->northWestY = boundingBox.y;
> tempFeature->box->southEastX = boundingBox.x+boundingBox.width;
> tempFeature->box->southEastY = boundingBox.y+boundingBox.height;
>
> tempFeature->roundness = diffpro;
>
> this->vectorFeatureModel.push_back( tempFeature );
>
> }
>
> </code>
>
> This produces a small memory leak - but still growing over time
> (so i have to get rid of it!)
>
> I m using the visual studio 6 STL implementation. I tested it with STLport
> but still got memory leaks!
>
> tested different <vector> approaches: with/without itertator, cleart
instead
> of erase...
>
> I got the same memory leak with a <list> approach
>
> I found a posting saying that the order of releasing memory/pointer is
> important, doesn't work for me!
>
>
> Is this probably a well known STL problem / could not find any posts
> What is wrong?
> Does anybody has the same experience using <vector> with pointers?

You'll be hard-pressed to find articles on STL containers and memory leaks.
A vector of pointers is extremely common although smart-pointers are
becoming commonplace.

>
>
> thanks in advance,
>
> Gerd
>
>



Relevant Pages

  • Re: Standard containers (Was: Wiki by the committee closed?)
    ... Since a fixed size is certainly enough for an iterator, ... bad pointers, overflows, and maybe even CONTAINER_ERROR_INDEX), why not ... be reported if a container is accessed out of bounds. ... OUR list elements never change when you add something to our lists. ...
    (comp.std.c)
  • Re: abstract containers: does such a thing exist, conceptually?
    ... create a firewall between different types of lists. ... > behaviour by the additional pointers. ... I do not like much STL's style of iteration with iterator objects, ... For example, an array view for containers with ordered elements, ...
    (comp.object)
  • Re: Pointer To A Vector Elements While Still Adding
    ... however there are cases that pointers get invalidated too in deque case. ... "surface" encapsulation about the type of the target container used, ... "surface" encapsulation can be provided by a typedef of the iterator type used. ...
    (comp.lang.cpp)
  • Re: Vector.Erase?
    ... So I tried to change it using iterator, but the result does not acting ... only on iterators, not pointers. ... vector::iterator was a pointer in the VC6 STL. ... gratuitously embed expressions with side-effects inside other expressions): ...
    (microsoft.public.vc.mfc)
  • Re: strange crash after assertion with std::map::iterator
    ... involving pointers, exhibit undefined behavior. ... assert with iterator debugging disabled? ...
    (microsoft.public.vc.stl)