I used boost::interprocess::managed_(windows_)shared_memory::construct to construct an interprocess vector holding an own class, which has a member variable of type std::string and another of type std::vector, so:
class myclass
{
public:
myclass()
{
}
std::string _mystring;
std::vector < int > _myintvector;
};
template < class _type >
struct typedefs
{
typedef boost::interprocess::managed_windows_shared_memory _memory;
typedef _memory::segment_manager _manager;
typedef boost::interprocess::allocator < _type, _manager > _allocator;
typedef boost::interprocess::vector < _type, _allocator > _vector;
};
typedef typedefs < myclass > tdmyclass;
int main ()
{
using namespace boost::interprocess;
managed_windows_shared_memory mem ( open_or_create, "mysharedmemory", 65536 );
tdmyclass::_vector * vec = mem.construct < tdmyclass::_vector > ( "mysharedvector" ) ( mem.get_segment_manager() );
myclass mytemp;
mytemp._mystring = "something";
mytemp._myintvector.push_back ( 100 );
mytemp._myintvector.push_back ( 200 );
vec->push_back ( mytemp );
/* waiting for the memory to be read is not what this is about,
so just imagine the programm stops here until everything we want to do is done */
}
I just did this for testing, I expected neither std::string nor std::vector to be working, yet, if I read it in from another process, std::string actually works, it contains the string I assigned. That really surprised me. The std::vector on the other side only partially works, the value returned by size() is correct, but the programm crashes if I want to access an iterator or by using operator[].
So, my question is, why is it that way ? I mean, I never actually read the STL implementations of the SDK of Visual Studio, but isn't std::string just an std::vector with extra functions suited for strings ? Don't they both use std::allocator - which would mean, that BOTH std::string and std::vector wouldn't work in shared memory ?
Googling for this doesn't really result into anything but boost::interprocess::vector, and thats not what I searched. So I hope someone can give me some details about whats going on ^^
PS: If I did a typo in the above code, please pardon me, I just wrote it right now in this pages editor and im kinda way too used to autocomplete of my IDE ^^