I m having problem with sharing vectors between processes. I can share the vector and i can even get the size of the vector from the different process, but when i use at function, program just crash.
struct B
{
std::vector<int> vec;
};
int main(int cArgs, char* ppszArgs[])
{
if (cArgs == 1) { //Parent process
//Remove shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove() { shared_memory_object::remove("MySharedMemory"); }
} remover;
//Create a shared memory object.
shared_memory_object shm(create_only, "MySharedMemory", read_write);
//Set size
shm.truncate(1000);
//Map the whole shared memory in this process
mapped_region region(shm, read_write);
//Write all the memory to 1
B* test = new B();
CopyMemory(region.get_address(), test, sizeof(B));
parentProcess(); -> this method just starts the child process
int index = 1;
while (true)
{
if(index < 2)
{
((B*)region.get_address())->vec.push_back(index);
}
++index;
}
}
else
{
//Open already created shared memory object.
shared_memory_object shm(open_only, "MySharedMemory", read_only);
//Map the whole shared memory in this process
mapped_region region(shm, read_only);
//Check that memory was initialized to 1
HANDLE mem = region.get_address();
while(true)
{
std::cout << ((B*)mem)->vec.at(0) << std::endl; -> if for example i put
lista.size(), then i will get the number of items in vector.
}
}
}
My question is it even possible to access vector elements from child process ?