Boost logo

Boost Users :

Subject: [Boost-users] Problem Using managed_shared_memory
From: med ennemri (med.ennemri_at_[hidden])
Date: 2010-03-18 07:32:32


Hi all

I have a problem using managed_shared_memory , I can only read strings that
has size less that 16 characters.

This is my server code

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/managed_shared_memory.hpp>
#include <utility>
#include <iostream>

int main ()
{
using namespace boost::interprocess;
typedef std::pair<std::string, int> FileState;

try{
shared_memory_object::remove("MySharedMemory");
managed_shared_memory segment(create_only, "MySharedMemory", 655360);

std::string str1 = "aaaaaaaaaaaaaaabbb";//client can only read <16 sized
strings
 std::string str2 =
"baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
std::string str3 =
"caaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

std::string *instance1 = segment.construct<std::string>("1")(str1);
std::string *instance2 = segment.construct<std::string>("2")(str2);

std::string *instance3 = segment.construct<std::string>("3")(str3);

while(segment.get_num_named_objects() > 0)
std::cout<<"Memory is ready"<<std::endl;
}
catch(...){
shared_memory_object::remove("MySharedMemory");
throw;
}
shared_memory_object::remove("MySharedMemory");
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is my client code
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstddef>
#include <utility>
#include <cassert>
#include <iostream>
#include <fstream>
int main (int argc, char *argv[])
{
using namespace boost::interprocess;

try{

managed_shared_memory segment(open_only, "MySharedMemory");
std::pair<std::string*, std::size_t> res;

int a = 1;
std::ostringstream oss;
oss<<a;

res = segment.find<std::string> (const_cast<char*>(oss.str().c_str()));
if(res.second != 0)
{
std::cout<<*(res.first)<<std::endl;
segment.destroy<std::string>(const_cast<char*>(oss.str().c_str()));
}

}
catch(...){
shared_memory_object::remove("MySharedMemory");
throw;
}
shared_memory_object::remove("MySharedMemory");
return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Best Regards



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net