Hi, I am trying to create a shared memory segment where I can share a STRING between 2 process, I tried using the following code:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <cstdlib>
#include <string>
#include <iostream>
using namespace boost::interprocess;
typedef allocator<std::string, managed_shared_memory::segment_manager> ShMemAllocator;
typedef vector<std::string, ShMemAllocator> MyVector;
int main(int argc, char** argv) {
struct shm_remove {
shm_remove() {shared_memory_object::remove("MySharedMemory"); }
~shm_remove() {shared_memory_object::remove("MySharedMemory"); }
}remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
const ShMemAllocator alloc_inst(segment.get_segment_manager());
MyVector *myVector = segment.construct<MyVector>("MyVector")(alloc_inst);
myVector->push_back("a");
int stop; //this line helps me wait until i run the other process
std::cin >> stop;
return (EXIT_SUCCESS);
}
at the "client" process:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <cstdlib>
#include <string>
#include <iostream>
using namespace boost::interprocess;
typedef allocator<std::string, managed_shared_memory::segment_manager> ShMemAllocator;
typedef vector<std::string, ShMemAllocator> MyVector;
int main(int argc, char** argv) {
managed_shared_memory segment(open_only, "MySharedMemory");
MyVector *myVector = segment.find<MyVector>("MyVector").first;
std::cout << "myVector size: " << myVector->at(0) << std::endl;
return (EXIT_SUCCESS);
}
When I run both I receive an error "Segmentation Fault, Core Dumped"...
Does anyone know how to solve this?
Thanks
Dann