Search code examples
c++c++11vectorshared-ptrdelete-operator

Vectors of std::shared_ptr Lose Data


I am attempting to build std::vectors and std::maps of std::shared_ptrs, but they seem to have a habit of losing data. By that, I mean that when I push std::shared_ptrs into them, some of them will delete the data they point to.

Specifically, the first std::shared_ptr to be pushed into a std::vector is deleted. This is confusing as pushing a std::shared_ptr into a std::vector should increment the reference count until the original std::shared_ptr goes out of scope, correct? It seems that no other std::shared_ptrs are deleted, but I cannot check until I get this issue resolved, as it checks the values by iterating.

I apologize if I have not explained myself well enough. I am still adjusting to this site.

EDIT: Here is the code for the two relevant classes. The problem should lie in the Load member functions. It is quite large, since it's a file parser, so I can't tell where the problem is at. Value is a member variable that is a std::vector<std::shared_ptr<NBT::Tag::Base>> for List and a std::map<std::string, std::shared_ptr<NBT::Tag::Base>> for Compound.


Solution

  • Run your program through valgrind to locate the source of the segfault.