I figured it out.

In the reader, I was using the wrong basic_string.

by accident, I was using the std::basic_string.   I should have been using boost::interprocess::basic_string.

I changed that.  Then it worked.




On Fri, Mar 7, 2014 at 12:37 PM, Sean K <sk92129@gmail.com> wrote:
I am new to boost so please pardon if this question has already been asked.

I was able to build a shared memory IPC client and server using "int" primitive type successfully.

I need to send a fixed number of "char" (primitive type).   At first I tried to define a struct of a fixed char 

struct {
      char data[1024];
} dataset;

typedef dataset mychararray;

and use this like how this example shows for 'int"

http://en.highscore.de/cpp/boost/interprocesscommunication.html#interprocesscommunication_managed_shared_memory

But for some reason it did not compile due to the boost::interprocess::managed_shared_memory.construct () not accepting that kind of datatype.

So, I looked at the basic_string implementation based on these two web sites.
http://prakharprakhar.blogspot.com/2009/12/boost-shared-memory-vector.html
http://en.highscore.de/cpp/boost/interprocesscommunication.html#interprocesscommunication_managed_shared_memory (basic_string variant)

I am built a reader and writer separate app. The reader will look for content.   If it does not find it, it will wait or sleep and try again, etc.   If it finds something it will try to print the basic_string content that I sent over.

I thought I had it correctly built but obviously it is not since the reader server crashes or cores when it tries to access the p.first content.


Below are the two programs (along with the build.sh which compiles each).  I am testing this as a 32 bit app  on a 64 bit redhat5 and 32 bit centos6.   


You  will notice in the strreader.cc that when I try to access the basic_string, that it crashes.   

I ran the reader (server) within the 32 bit eclipse debugger, and the memory addresses and content of the p BStringPair looks fine.   Anybody have some ideas or can anybody try this and see what you can tell what is wrong.

strreader.cc



#include <iostream>



#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/thread/thread.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
using namespace boost::interprocess;


int main() {

typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef basic_string<char, std::char_traits<char>, ShmemAllocator> BString;

typedef std::pair<BString *, std::size_t> BStringPair;

int count = 0;
shared_memory_object::remove("Highscore");
while (true) {
managed_shared_memory segment(open_or_create, "Highscore", 65536);

if (segment.get_num_named_objects() > 0){
BStringPair p = segment.find<BString> ("str");
printf("stuff in memory\n");

if (p.first ){

                                // THIS IS WHERE IT CRASHES!!!
//printf("%s \n",(p.first->c_str()) );   
//int strlen = p.first->size();
}
printf("p.second: %d \n", p.second);
}

shared_memory_object::remove("Highscore");
//printf("one: %s", one.first);
//printf("two: %s", two.first);

    int msecs = 2000;
boost::this_thread::sleep(boost::posix_time::milliseconds(msecs));
printf("sleeping...\n");
    }


return 0;
}


build.sh for the reader

g++ -m32 -fPIC -Xlinker -zmuldefs  strreader.cc -o strreader -I/common/COTS/boost_1_42_0/include -L/common/COTS/boost_1_42_0/lib -lboost_thread -lboost_system -pthread


strwriter.cpp


#include <iostream>



#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/thread/thread.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

#include <iostream>
#include <string>
#include <cstdlib>


using namespace std;
using namespace boost::interprocess;


int main() {

typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, ShmemAllocator> BString;


try {
shared_memory_object::remove("Highscore");
printf("cleared out memory\n");


managed_shared_memory segment(open_or_create, "Highscore", 65536);

printf("initialized managed shared memory \n");
ShmemAllocator allocInst (segment.get_segment_manager());
BString bstrData(segment.get_segment_manager());
bstrData = "hello";

std::string data = "Hello";
//BString bstrToSend(data.begin(), data.end());
//BString bstrToSend(data.c_str());

BString *mystring = segment.construct<BString> ("str")(bstrData);

//mystring="Hello";

printf("inserted mystring into shmvector \n");
} catch (boost::interprocess::bad_alloc e) {
printf("we caught bad alloc\n");
printf("explanation: %s", e.what());
throw;
} catch (...) {
printf("caught unknown exception\n");
throw;
}

printf("wrote number");
return 0;
}


build.sh for the writer:
g++ -m32 -fPIC -Xlinker -zmuldefs  strwriter.cpp -o strwriter -I/common/COTS/boost_1_42_0/include -L/common/COTS/boost_1_42_0/lib -lboost_thread -lboost_system -pthread

--
Sean



--
Sean