Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving nested BoostStores by pointer #13

Open
marc1uk opened this issue May 31, 2022 · 0 comments
Open

Retrieving nested BoostStores by pointer #13

marc1uk opened this issue May 31, 2022 · 0 comments

Comments

@marc1uk
Copy link
Contributor

marc1uk commented May 31, 2022

if you try to read a BoostStore from file using a pointer (BoostStore::Get(std::string name, T*& ptr)) it will automatically instantiate a BoostStore on the heap for you, but doesn't necessarily create the correct type (binary, text, multi-entry) of BoostStore, which can result in a segfault when you try to use the corresponding object.

So normally one can do, for example:

// read a std::vector<double> from a BoostStore called MyBoostStore, stored with the key 'mydoubles'
std::vector<double>* somevector;
MyBoostStore->Get("mydoubles", somevector);
std::cout<< somevector.size() << std::endl;   // works just fine

and that would work. The BoostStore would construct a std::vector on the heap (which it owns), and set your pointer somevector to point at it.
Unfortunately this doesn't necessarily work to retrieve a BoostStore which is embedded in another BoostStore:

// read a type-2 (multi-entry) BoostStore nested within MyBoostStore with the key 'myBoostStore'
BoostStore* somebs;
MyBoostStore->Get("myBoostStore", somebs); 
somebs->Print(false);      // segfaults; somebs is malformed.

A workaround is to create the local BoostStore manually, with the correct type, and retrieve it by value rather than with a pointer:

BoostStore somebs(true, 2);     // true= type-checking enabled, 2= mutli-entry binary boost-store. These must match the type of BoostStore you're retrieving.
MyBoostStore->Get("myBoostStore", somebs);
somebs.Print(false);            // okay!

but this caught mattw out as his code was using pointers, and took me a little bit to debug because simply looking at the code there doesn't immediately appear to be anything syntactically incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant