[Boost::Interprocess] access violation when function returns a pointer to shared memory object

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

I think that you are having a problem because your "segment" object has gone out of scope. I believe that the memory is unmapped at that point, and you are left with a dangling pointer. ~Dan
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of BaDBuTz Sent: Thursday, October 22, 2009 5:32 To: boost-users@lists.boost.org Subject: [Boost-users] [Boost::Interprocess] access violation when function returns a pointer to shared memory object
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; }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
BaDBuTz
-
Casimiro, Daniel C CIV NUWC NWPT