hi,
I want to synchronize multiple Processes with boost::Interprocess.
For the start I implemented a simple Synchronisation Class (with some mutexes) in my test program that will be stored in the Shared Memory.
The first call of print() (in function getSyncObject) returns the correct output. But why do I get an access violation, when I call the function again (the pointer is the same!) in main()?
I am using WINXP, msvc 7-1 and mingw44 (with both compilers I get the same error)
Thanks in advance
Martin
(compilable) Code:
#include <iostream>
#include
#include
#include
typedef unsigned long U32;
class SyncObject
{
public:
SyncObject(std::string aId) : id(aId),
locAddrSp0Range(0),
locAddrSp0Remap(0),
locAddrSp1Range(0),
locAddrSp1Remap(0),
usbInterfaceOwner(0),
useCounter(0)
{};
~SyncObject()
{}
;
void print()
{
std::cout
<< id
<< " locAddrSp0Range = " << locAddrSp0Range
<< " locAddrSp0Remap = " << locAddrSp0Remap
<< " locAddrSp1Range = " << locAddrSp1Range
<< " locAddrSp1Remap = " << locAddrSp1Remap
<< " usbInterfaceOwner = " << usbInterfaceOwner
<< " useCounter = " << useCounter
<< "\n";
};
std::string id;
boost::interprocess::interprocess_mutex mtxReadWrite;
boost::interprocess::interprocess_mutex mtxFunction;
U32 locAddrSp0Range;
U32 locAddrSp0Remap;
U32 locAddrSp1Range;
U32 locAddrSp1Remap;
U32 usbInterfaceOwner;
U32 useCounter;
};
class SharedMemory
{
public:
SharedMemory();
~SharedMemory();
SyncObject* getSyncObject(std::string id);
};
using namespace boost::interprocess;
const char* MANAGED_SHARED_MEMORY_NAME = "sharedMemoryMapTest17";
SharedMemory::SharedMemory()
{}
SharedMemory::~SharedMemory()
{}
SyncObject* SharedMemory::getSyncObject(std::string id)
{
managed_shared_memory segment(open_or_create, MANAGED_SHARED_MEMORY_NAME, 65536);
SyncObject* ptr = segment.construct<SyncObject>(anonymous_instance)(id);
ptr->print(); //works!
return ptr;
}
int main()
{
SharedMemory shm;
SyncObject* ptr = shm.getSyncObject("idName");
ptr->print(); //Application crashes here with an access violation
return 0;
}