Boost memory mapped object

i've created dll and implemented shared memory that every connected process can use. My problem is that i can't change anything in object, which is stored in the memory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
my class :

class MyClass
{
public:
    MyClass();
    void test();
    int counter;
};

void MyClass::test() {
    MessageBoxA(NULL, "test", "test", 0x0000000L);
    counter++;
}

in stdafx.h i have :

1
2
static offset_ptr<MyClass> offset_mt;
static managed_shared_memory *memSegment;


I initialize shared memory and pointer :

1
2
memSegment = new managed_shared_memory(create_only, SHARED_MEMORY_NAME, 4096);
offset_mt = memSegment->construct<MyClass>("MyClass myClass")();


And then in an exported function i call

offset_mt.get()->test();

Im calling this from Java using JNA and result is a memory error (Invalid memory access). However, if I delete 'counter++' from test method, everything works fine - message box appears. Is there a limitation that I cant modify objects inside mapped memory or is this done the other way?
Well, without the counter++, MyClass::test() doesn't use this at all, so it sounds like get() is returning garbage or nullptr.
Well, i solved this by moving my variables to stdafx.cpp :

1
2
offset_ptr<MyClass> offset_mt;
managed_shared_memory *memSegment;


and making them extern in stdafx.h :

1
2
extern offset_ptr<MyClass> offset_mt;
extern managed_shared_memory *memSegment;


Now it's running fine, but I've done this kinda accidentally and I'm not pretty sure why this works and previous way not. If anyone could explain this to me, it would be great.
Last edited on
Ah, yes. Sorry, I missed that the first time.

static globals are translation-unit-local. If you put a static global named foo in a header, every translation unit that includes that header will get its own object named foo that is separate (i.e. has a different memory address) from every other translation unit's foo.
Topic archived. No new replies allowed.