Reputation: 437
I am trying to use a ptr_vector to store some pointers, but I get an error as soon as my main method. Here is my code:
int main(void)
{
boost::ptr_vector<string> temp;
string s1 = "hi";
string s2 = "hello";
temp.push_back(&s1);
temp.push_back(&s2);
return 0;
}
This is the error message I am getting:
Critical error detected c0000374
Windows has triggered a breakpoint in Path_Tree.exe.
This may be due to a corruption of the heap, which indicates a bug in Path_Tree.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Path_Tree.exe has focus.
The output window may have more diagnostic information.
The program '[7344] Path_Tree.exe: Native' has exited with code 0 (0x0).
What am I doing wrong? Thanks!
Upvotes: 1
Views: 464
Reputation: 227390
ptr_vector
is designed to store pointers to dynamically allocated objects. It then takes ownership of them and deletes them at the end of its lifetime. If you pass it pointers to objects that cannot be deleted, then you get undefined behaviour.
int main(void)
{
boost::ptr_vector<std::string> temp;
temp.push_back(new std::string("hi"));
temp.push_back(new std::string("hello"));
return 0;
}
Upvotes: 1
Reputation: 40603
ptr_vector
takes ownership of the objects pointed to by the pointers you give to it (meaning that it calls delete
on those pointers when it is done with them). If you want just a vector of pointers, use:
int main()
{
std::vector<string*> temp;
//s1 and s2 manage their own lifetimes
//(and will be destructed at the end of the scope)
string s1 = "hi";
string s2 = "hello";
//temp[0] and temp[1] point to s1 and s2 respectively
temp.push_back(&s1);
temp.push_back(&s2);
return 0;
}
otherwise, you should allocate your strings using new, and then push_back the resulting pointers:
int main()
{
boost::ptr_vector<string> temp;
temp.push_back(new string("hi"));
temp.push_back(new string("hello"));
return 0;
}
If you want just a container of strings, the usual thing would be a vector of strings:
int main()
{
std::vector<string> temp;
//s1 and s2 manage their own lifetimes
//(and will be destructed at the end of the scope)
string s1 = "hi";
string s2 = "hello";
//temp[0] and temp[1] refer to copies of s1 and s2.
//The vector manages the lifetimes of these copies,
//and will destroy them when it goes out of scope.
temp.push_back(s1);
temp.push_back(s2);
return 0;
}
ptr_vector
is meant to make it easier to have polymorphic objects with value semantics. If your strings are not polymorphic in the first place then ptr_vector
is totally unnecessary.
Upvotes: 10
Reputation: 9841
There is no point of using ptr_vector if your not going to be pushing back a pointer into it.
Either use std::vector or push back a string that is allocated with the new keyword.
Upvotes: 0