I have following problem:
When I use std::vector with built-ins i don't get a memory but if I use classes I get memory leak. To illustrate:
//No leak
std::vector<double>* vecPtr1=new std::vector<double>();
//add some elements
delete vecPtr1;
//Leaks some memory but not all
std::vector<SomeClass>* vecPtr2=new std::vector<SomeClass>();
//add some elements with vecPtr2->push_back(SomeClass());
delete vecPtr2;
As far as I understand it delete should call the destructor of std::vector which should in turn call the destructor of SomeClass -> no leak. I have invested some thought and testing into this and the same behaviour happens if I use std::vector in a scope such as here:
{
std::vector<SomeClass> vector;
//add elements as before
}
//memory is still used here
I'm using gcc 4.6.1 under Ubuntu 11.10. Is something amiss in my library or do I have a misconception how std::vector destructs elements?
For clarification my complete code with SomeClass replaced with std::pair (yes I know some parts are hacked but it's just an example):
#include <iostream>
#include <vector>
#include <utility>
int main()
{
std::string inString;
std::cout<<"Started"<<std::endl;
//wait
std::cin>>inString;
{
//assign vector
std::vector<std::pair<std::string,unsigned int> > vec=std::vector<std::pair<std::string,unsigned int> >();
//push elements
for(unsigned int i=0;i<1e7;++i)
{
vec.push_back(std::pair<std::string,unsigned int>("something",i));
}
std::cout<<"Created vector with capacity: "<<vec.capacity()<<std::endl;
//wait
std::cin>>inString;
}
//vec should go out of scope but not all memory gets freed
std::cout<<"Deleted vector"<<std::endl;
//wait
std::cin>>inString;
std::cout<<"Shutting down"<<std::endl;
return 0;
}