Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak (sort of) with a static std::vector

I have a static std::vector in a class. When I use Microsoft's memory leak detection tools:

_CrtMemState state;
_CrtMemCheckpoint( & state);
_CrtMemDumpAllObjectsSince( & state );

it reports a leak after I insert stuff into the vector. This makes sense to me because new space is allocated when something is inserted into the vector. This space isn't deallocated until the program terminates (since the vector is static). Is this right?

In the destructor of the class that contains the vector, I'm deleting the object that I put into the vector. However, the memory that's allocated when the insertion happened is still hanging around. Is there anyway to delete this space?

like image 233
Joe Avatar asked May 22 '09 14:05

Joe


1 Answers

You can swap the vector with an empty one - this will release the memory.

See also Q: Shrinking a vector

like image 199
James Hopkin Avatar answered Oct 05 '22 23:10

James Hopkin