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!
ptr_vectortakes ownership of the objects pointed to by the pointers you give to it (meaning that it callsdeleteon those pointers when it is done with them). If you want just a vector of pointers, use:otherwise, you should allocate your strings using new, and then push_back the resulting pointers:
If you want just a container of strings, the usual thing would be a vector of strings:
ptr_vectoris meant to make it easier to have polymorphic objects with value semantics. If your strings are not polymorphic in the first place thenptr_vectoris totally unnecessary.