c++ - MapViewOfFile returns different adresses with same handle -
im trying implement ipc school assignment sharing memory. made class called sharedmemorybuffer
deal creating file mappings , views.
my init()
function looks this:
byte * sharedmemorybuffer::init(const wchar_t * name, size_t buffersize) { filemaphandle = openfilemapping( file_map_all_access, // read/write access false, // not inherit name name); // name of mapping object if (filemaphandle == null) { filemaphandle = createfilemapping(invalid_handle_value, null, page_readwrite, 0, buffersize, name); pbuf = (byte*)mapviewoffile(filemaphandle, // handle map object file_map_all_access, // read/write permission 0, 0, buffersize); } else { pbuf = (byte*)mapviewoffile( filemaphandle, file_map_all_access, 0, 0, buffersize ); } return this->getbuffer(); }
essentially, pass name , size , tries open mapping name. if fails, creates instead.
i call this->ringbuffer.init(widestr.c_str(), buffsize);
after done (i call init 4 times 2 buffers, same process) print out adresses of buffers (pbuf
init()
) theyre different addresses.
i cant love of life figure out why adresses different! have made sure second time call init()
same name indeed open file mapping successfully.
any appreciated!
you mapping same region twice in process. 2 distinct addresses, backed same physical memory. writing buffer pointed first address modifies buffer pointed second address, since same memory.
Comments
Post a Comment