I have been messing around with using smart pointers in my latest C++ endeavor for memory management. Part of this is that I am using a series of maps to tie a string to a class (called an IBlockInput) and the class to its current value inside a “context”. I see two options, but both have the same problem:
Option 1: The class’s current value is stored inside the class, sorted by context
Option 2: The class’s current value is stored inside the context, sorted by class
The problem here is that either object may die before the other. A Context may be destroyed while all the inputs remain or an input may be destroyed while the context remains. When one dies, the resources need to be released for it. What I have so far is (using option 2):
std::map<std::string, boost::weak_ptr<IBlockInput> > inputs;
std::map<boost::weak_ptr<IBlockInput>, boost::shared_ptr<std::vector<double> > > inputValues;
The reason they need to be separate like this is that other classes (IOutputBlocks) need to access and set the values for the IInputBlocks based on their pointer.
There are a few questions I have about this:
Question 1: Can I access the inputValues map like a normal map and just pass in the pointer (not the boost::shared_ptr/boost::weak_ptr…the T*) to access it? I can use weak_ptrs or shared_ptrs as well if that is the actual way to do it
Question 2: I am using weak pointers to let the objects die if needed (If I was doing option 1 above, I would have to weak pointers pointing to the context). What happens if the weak pointer expires and I try to access it in the map? Is the data it points to lost to the ages or does it still work with the raw pointer?
Question 3: Perhaps a better way to do this is to use the raw pointers as the keys/values and then have a supplementary map with the weak pointers (for a live check of sorts) like so:
std::map<std::string, IBlockInput*> inputs;
std::map<IBlockInput*, boost::shared_ptr<std::vector<double> > inputValues;
std::map<IBlockInput*, boost::weak_ptr<IBlockInput> > pointerCache;
Then I could do a “live check” using the pointerCache to see if I should keep the vector of doubles alive and manage the memory that way. NOTE: The inputs vector is refreshed relatively often.
What is the recommended practice for this? I am very new to boost and using smart pointers in general (I’ve been pampered by C# at work for the past several months and not had to worry about this kind of thing).
std::map<boost::weak_ptr<IBlockInput>, boost::shared_ptr<std::vector<double> > > inputValues;— this is bad. When aweak_ptrdies, it will compare equal tonullptr, and its order is not maintained. You’ll get undefined behavior (and in practice, infinite loops and crashes)Be careful what you use for keys in
std::maps.Using raw pointers that may be deallocated is no good due to the ABA problem, where a valid pointer becomes invalid, then a new pointer is allocated equal to it. (there is exploiting implementation details of
make_sharedto make this kind of thing work in practice, but that is messy and dangerous)You probably need some kind of double-indirection, where you have the equivalent of pointers to pointers. I’ll call them handles. The inner pointer of the handle expiry causes it to unregister the handle in maps, and when it is out of every map it destroys itself. I don’t know if it will work, but it might. I suspect it would need some fiddling first.
Handles would be ordered by their (outer) pointer value, so would maintain ordering. To determine if a handle is valid, you have to both check the outer and inner pointer’s validity (so non-null handles can be invalid to “dereference”).
But I cannot think of a way to make this clean.