I am trying to write a simple program which listens to some data on network and writes to a file. I decided to use mmapped files because the data is very large (magnitude of 3-5 Gigs) and very fast. so as expected, i put the data in queue and another thread pops it out and writes into the mmap file.
this is my first time to venture into windows based C++ programming. I maybe missing something basic with Shared memory mapped files here. could you someoen suggest me what configuration do i have to use to make it work? I think i am a little confused about the buffer size, and other parameters like maximum object size (high-order DWORD), maximum object size (low-order DWORD) etc.

Following is the code snippet in my program ..

#define BUF_SIZE1 4096*1024 // 4 mb buffer

DWORD dwflags;
unsigned long lowDWordVal = 0;
unsigned long highDWordVal = 0;
unsigned long dwordSize = 4294967294;


hMapFile = CreateFileMapping(m_hFile,    // use paging file
                NULL,                    // default security 
                PAGE_READWRITE,          // read/write access
                2,                       // maximum object size (high-order DWORD) 
                dwordSize,                // maximum object size (low-order DWORD)  
	 "Outputmmap");                 // name of mapping object

pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object
	FILE_MAP_ALL_ACCESS, // read/write permission
         BUF_SIZE1);	//BUF_SIZE);

Above is the initialization part which maps the file correctly. After this another funcion which writes to a mmap file. .

I will skip the code which writes to mmap file. All it does is maintain an index position in the file and when it seaches the length BUF_SIZE1 it unmaps the file and then maps it to the index+1 position.

This code also worked fine, but everytime i run the program it writes some amount of data and after sometime it crashes. It crashes everytime with error Could not write at memory locates 0x xyz.. I believe something to do with incorrect mapping.

Could someone please advice? Anything specifc that I should look at?


6 Years
Discussion Span
Last Post by mike_2000_17

You should find out where this error occurs:
Is it before writing the first chunk? -something wrong with init.
Is it right when you switch to second chunk? -something wrong with the switch.
Is there anything special about the point at which the error occurs (middle of a chunk or at a switch, and which switch)? -something is winding out of scope.
Or does it occur at random times? -thread synchronization issue, almost certainly.

So check to make sure your writing operation is not going beyond the current buffer (in my experience, problems like that are usually due to a little typo somewhere).

I assume this is multi-threaded (one thread pushes, the other pops), so make sure to use a mutex to protect the buffer, especially during the switch.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.