Re: memory leak in <vector> STL
From: SaltPeter (SaltPeter_at_Jupiter.sys)
Date: 07/20/04
- Next message: Igor Tandetnik: "Re: memory leak in <vector> STL"
- Previous message: tom_usenet: "Re: memory leak in <vector> STL"
- In reply to: Gerd Keller: "memory leak in <vector> STL"
- Messages sorted by: [ date ] [ thread ]
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
>
>
- Next message: Igor Tandetnik: "Re: memory leak in <vector> STL"
- Previous message: tom_usenet: "Re: memory leak in <vector> STL"
- In reply to: Gerd Keller: "memory leak in <vector> STL"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|