Boost logo

Boost Users :

Subject: [Boost-users] [Boost::Interprocess] access violation when function returns a pointer to shared memory object
From: BaDBuTz (badbutz_at_[hidden])
Date: 2009-10-22 05:31:54


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 <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>

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 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