1

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!

Qman
  • 377
  • 1
  • 4
  • 14

3 Answers3

10

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.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
1

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;
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Might be that it does automatic cleanup, which could explain why it crashs. – Sebastian Hoffmann Aug 15 '12 at 15:21
  • Don't write this code (in the general case where the code is not entirely contained in main). If `new std::string("hello")` throws, the string created by `new std::string("hi")` will not be deleted, and if `temp.push_back(s1)` throws, the string created by `new std::string("hello")` will not be deleted. – Mankarse Aug 15 '12 at 15:29
  • @Mankarse good point. I was only trying to illustrate the problem behind the heap corruption. I changed the code, but I don't know what guarantees `boost::ptr_vevtor::push_back` has if the `new` expressions throw. – juanchopanza Aug 15 '12 at 15:36
  • @juanchopanza: `push_back` immediately takes ownership of the supplied pointer, and will release the supplied pointer if the `push_back` operation throws. If the `new` throws, `push_back` is never reached, and so is irrelevant. – Mankarse Aug 15 '12 at 15:39
0

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.

Caesar
  • 9,483
  • 8
  • 40
  • 66