Reputation: 743
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.
Upvotes: 0
Views: 2442
Reputation: 24174
Run your program through valgrind to locate the source of the segfault.
Upvotes: 2
Reputation: 146910
You can't just construct shared_ptr's willy-nilly, they need to be constructed from each other, else they think they're the only owning shared_ptr and delete the object when they go out of scope. If you want to be able to construct shared_ptr's directly from an object, you need to inherit from enable_shared_from_this<Type>
. I can't read your code easily (there's a lot there), but your symptoms sound to me like you've been misconstructing them.
Upvotes: 2