Help with custom classes PLEASE!!

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: *In the "main" 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) { 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

AMDG Daniel Veneros wrote:
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:
*In the "main" 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;
You can't use std::string. You need to use boost::interprocess::basic_string with an appropriate allocator (just as you need to use boost::interprocess::vector instead of std::vector). In Christ, Steven Watanabe

Hi, Thanks, this works now... I got one othr question, what if I want to share a MAP type between both processes? a map that has a string as a key and a custom class as a value? I mean: class Employee { private: string name; int age; public: Employee(); ~Employee(); }; map<string, Employee> Somehing like that... how can I implement that? I tried doing this: using namespace boost::interprocess; map<std::string, Employee> myMap; typedef allocator<myMap, managed_shared_memory::segment_manager> ShMemAllocator; typedef vector<myMap, ShMemAllocator> MyVector; But it's not working... THANKS!!! Dann On Mon, Feb 1, 2010 at 5:42 PM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
Daniel Veneros wrote:
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:
*In the "main" 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;
You can't use std::string. You need to use boost::interprocess::basic_string with an appropriate allocator (just as you need to use boost::interprocess::vector instead of std::vector).
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

And what about the class? Do i just need to create allocators for the map and the string(the map's key)?? typedef allocator<basic_string, managed_shared_memory::segment_manager> StrAllocator; typedef Employee Value; typedef std::pair<StrAllocator, Employee> ValueType; typedef allocator<ValueType, managed_shared_memory::segment_manager> ShMemAllocator; typedef map<StrAllocator, Value, std::less<StrAllocator>, ShMemAllocator> MyMap; Is this correct? Thank you very much again... Dann On Mon, Feb 1, 2010 at 5:59 PM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
Daniel Veneros wrote:
I got one othr question, what if I want to share a MAP type between both processes? a map that has a string as a key and a custom class as a value?
The map and the string both need to have allocators.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

AMDG Daniel Veneros wrote:
And what about the class?
Oh, I see.
class Employee { private: string name; int age;
public: Employee(); ~Employee(); };
The string inside the class needs to use an allocator too. Everything that allocates memory which you store in shared memory needs to use the right allocator. In Christ, Steven Watanabe
participants (2)
-
Daniel Veneros
-
Steven Watanabe