Skip to content

Retrieving nested BoostStores by pointer #13

@marc1uk

Description

@marc1uk

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions