[RESOLVED] Error on destruction of boost::shared_ptr
I have an stl vector of boost::shared_ptr's declared like so:
vector< shared_ptr<delim_list> > row_data;
delim_list is a custom class encapsulating a vector of strings.
The container is filled like this:
shared_ptr<delim_list> insert_list(new delim_list(line, ','));
row_data.push_back(insert_list);
When the row_data vector goes out of scope there is a access memory reading violation thrown from the std::string library.
I am using VS2005 and am quite at a loss. The code worked when I was using raw pointers, but am now trying to use smart pointers to save time and gain security.
PS: Are there any code tags for these posts?
Thanks,
Ian
Re: Error on destruction of boost::shared_ptr
Yes there are code tags.
You probably corrupted an object somewhere with an out-of-bounds write.
I *have* seen a case where a shared pointer to an object containing another shared pointer and so on (basically a linked list) errored on destruction due to stack overflow trying to destroy all the shared_ptrs. But if that isn't your problem then it's probably some form of corruption.
Re: Error on destruction of boost::shared_ptr
Okay,
When you say corruption you mean something perhaps trying to destruct an object twice? Like perhaps the delim_list destructor of the elements of the vector get called before the shared_ptr goes out of scope, or vis-versa.
Just for clarity this is the delim_list classes declaration;
Code:
class delim_list
{
private:
char delimiter;
vector<string> delim_vec;
string delim_str;
public:
delim_list(const string& str, char delim);
delim_list(vector<string>& vec, char delim);
delim_list(char delim);
const string& to_string(){return delim_str;}
const vector<string>& to_vector(){return delim_vec;}
void push_back(const string& str);
};
And here is the full code that loads it
Code:
//This is a virtual function called from the constructor of CSV_File.
void CSV_File::load_data(ifstream& csv_file)
{
string line;
string cell;
//loop through lines in a file and add lines to row_data
while (getline(csv_file,line))
{
stringstream line_stream(line);
counted_ptr<delim_list> insert_list(new delim_list(line, ','));
row_data.push_back(insert_list);
}
}
So no shared pointer in whats being pointed to. So it must be some sort of corruption. I was a little worried about creating the string 'line' on the stack and then passing it by reference into the constructor for delim_list to create the vector elements, but I think its okay.
Re: Error on destruction of boost::shared_ptr
It looks like I misplaced the location of the error, but its still puzzling. I have a std::auto_ptr to that delim_list class that I declare as a class variable and set during construction. But it is inaccessible after that.
The part of the constructor that sets it looks like this.
Code:
//variables line and cell are local to the constructor
string line;
string cell;
getline(csv_file, line);
//load the line into a string stream
stringstream line_stream(line);
header = new delim_list(line, ',');
When I call the to_string method (posted earlier) of the delim_list object referenced by the auto_ptr, I get access memory violations. This same problem causes the code to throw an error when the auto_ptr goes out of scope.