Question

I am learning working with shared memory in C++. I found that under Windows I need to use CreateFileMapping and MapViewOfFile functions. I want to share array of char so part of my code is:

HANDLE hBuffer = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, bufferName);
char * buffer = (char *) MapViewOfFile(hBuffer, FILE_MAP_ALL_ACCESS, 0, 0, size);

(there is checking for NULLs of course) and at the end of using shared memory I call:

UnmapViewOfFile(buffer); // returned true
CloseHandle(hBuffer); // returned true also

But in Resource monitor I can see that there was no memory released. When it is called several times, the allocated memory of application is increasing but there is no releasing. What am I doing wrong? Or is there another function to release shared memory?

Thanks for answers.

Was it helpful?

Solution

Problem solved thanks to marcin_j:

Your code looks find (well,... you mispelled HANDLE), you can use procexp.exe from sysinternals to find your HANDLE by name (if it is not found then it was closed), also observe on Performance tab how Virtual Size of you app changes, there is also Handles count that should change accordingly.

Also, observe what will happen after you execute memset(buffer,0,size); after MapViewOfFile - this is actually when system will commit memory and when your workin set will rise.

My above comment is wrong, CreateFileMapping by default applies SEC_COMMIT which commits memory. But I suppose your memory is paged until memset is called, after that call paged pages are moved to physical memory, which rises working set.... if I am not wrong...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top