How to keep a pointer on a shared data in a boost shared memory segment ?
I have a function which returns:
shm.construct<SharedData>(_nameSeg.c_str())(innerDataAllocator);
But outside of the function, the object pointed by SharedData is inaccessible. Why ?
My general problem is that I want to have a way to get, and set data in shared memory, but without, each time, having to :
- get shm
- find object in shm based on its name
- construct allocators.
So what I'm trying to do is to store in a struct:
- allocators
- a pointer to the shared object
Is it a good idea/design ? How could I do this ?
EDIT
Here is the type of class I would like to have, it's a singleton storing the info of the current shm:
typedef bip::allocator<wchar_t, bip::managed_shared_memory::segment_manager> CharAllocator;
typedef bip::basic_string<wchar_t, std::char_traits<wchar_t>, CharAllocator> MyShmString;
class InnerData {
public:
uint64_t id;
MyShmString name;
uint8_t status;
static const uint8_t defaultStatus = 3;
InnerData(CharAllocator cAlloc):name(cAlloc) {
}
};
typedef bip::allocator<InnerData, bip::managed_shared_memory::segment_manager> InnerDataAllocator;
class Zone {
public:
SharedData * sharedData;
CharAllocator charAllocator;
InnerDataAllocator innerDataAllocator;
Zone():{} //here I need to initialize charAllocator and innerDataAllocator with shm.get_segment_manager(),
// because bip::allocators don't have default constructors
static Zone create(std::string &_name, std::string &_nameSeg);
static Zone get(std::string &_name, std::string &_nameSeg);
static Zone delete(std::string &_name, std::string &_nameSeg);
void putInShm(const std::vector<UsualInnerData> &in);
static bool alreadyCreated;
};
in .cpp
Zone::create(std::string &_name, std::string &_nameSeg){
//create the shm
// create allocators charAllocator and innerDataAllocator, passing shm.get_segment_manager()
//construct the sharedData object (with shm.construct<SharedData>(_nameSeg.c_str())(innerDataAllocator))
//create a Zone
Zone z(charAllocator, innerDataAllocator, sharedData );
return z;
}
In this case, the Zone constructor would be:
Zone(CharAllocator _charAllocator, InnerDataAllocator _innerDataAllocator, SharedData* _sharedData):
charAllocator(_charAllocator),
innerDataAllocator(_innerDataAllocator),
sharedData(_sharedData)
{}
But then, as I said, once we quit Zone::Create, the data to where the SharedData pointer (which is inside the returned Zone z) points to, is de-allocated memory.
And the second problem is in Zone::putInShm:
void Zone::putInShm(const std::vector<UsualInnerData> &in){
InnerData newInnerData (charAllocator);
MyShmString newShmString(charAllocator)
for (auto a : in) {
newInnerData.id = a.id;
newShmString = a.name.c_str();//Here I get an access violation, which means charAllocator is bad ?
newInnerData.name = newShmString
sharedData->data.push_back(newInnerData);
}
return;
}
EDIT 2:
Here is a minimal, compilable code. It reproduces the errors described above: https://uploadfiles.io/8o8zo password: stackoverflow