Link to home
Start Free TrialLog in
Avatar of DBTechnique
DBTechnique

asked on

MapViewOfFile returns 8 : How to solve ?

Hello experts,
I am trying to allocate a shared memory with MapViewOfFile and it returns NULL with error code 8. As found on internet, I understand that I don't have enough memory available, or not enough contiguous memory available to allocate. The size of the memory I try to allocate is 200 MBytes. My PC has 2 GBytes total of RAM and I can see that with all the other processes opened, I still have more than 900 MBytes available. My program is using around 160 MBytes.
I am guessing that it should not failed.
Am I missing something ?
Thanks.
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Post here the code.
Actually, this is the classical example in MSDN:
Creating Named Shared Memory
http://msdn.microsoft.com/en-us/library/aa366551(VS.85).aspx
It makes sense to check the virtual memory: Settings -> System ->Advanced and the second page here, "total paging file size".
Avatar of DBTechnique
DBTechnique

ASKER

Hello,
I have added my code below.
Also I checked the total paging file size, it is 2069 MB.
if (m_bMappingFileOnHardDisk)
   hFile = CreateFile(csFileName, 
                        GENERIC_READ | GENERIC_WRITE, 
                        FILE_SHARE_READ, 
                        NULL, 
                        CREATE_ALWAYS, 
                        FILE_ATTRIBUTE_NORMAL, 
                        NULL);
else
   hFile = NULL;

stFileSizeLowHigh* stSize = (stFileSizeLowHigh*) &llSharedMemorySize;

hMapObject = CreateFileMapping(m_hFile,
                                NULL
                                PAGE_READWRITE, 
                                stSize->dwHigh,
                                stSize->dwLow,
                                csSharedMemoryName);
		
if (hMapObject == NULL)
   return FALSE;
		
fInit = (GetLastError() != ERROR_ALREADY_EXISTS);
lpvMem = MapViewOfFile(m_hMapObject,FILE_MAP_ALL_ACCESS,0,0,0);
		
if (m_lpvMem == NULL)
   return FALSE;

if (fInit)
   memset(m_lpvMem, '\0', m_llSharedMemorySize);

return TRUE;

Open in new window

Avatar of jkr
How are you initializing 'stSize'?
And what is this "csSharedMemoryName"?
Why you don't want to verify your code with the example I posted?
hMapObject = CreateFileMapping(m_hFile,
                                NULL
                                PAGE_READWRITE,  
                                0,
                                nSize,
                                csSharedMemoryName);
where DWORD nSize = 1024 * 1024 * 200; in your case.
I think this is the fix.
Hello,
So first, thanks for the reply.
stSize is a structure with two DWORD. But in the reality, stSize->dwHigh is always equal to 0.
csSharedMemoryName is the name associated with the shared memory.
In my application, it is set to _T("SHARED_A_MEMORY_0").
I tried your solution pgnatyuk, I got the same error.
However, I have noticed a difference in the code from your past link.
In this code, I can see that if MapOfViewFile failed, there is a call to CloseHandle for hFile.
This is missing in my code. Do you think this can be a problem ?
You need to call CloseHandle, when you don't need the handle. So fix the code, reset the computer and test the application again. What is the file name?
 
 
Ok. I will test that. Thanks.
About the file name, do you mean the path and name of the file mapped on hard disk ? If so, I don't use one.
I map only in RAM.
The object name for the shared memory is as I said before _T("SHARED_A_MEMORY_0").
I just tried and it fails. However it works on another PC (failed on my laptop, work on another desktop PC).
The two PCs have the same amount of RAM, and there are at least 1GBytes available on both.
Just to be clear, since I am not using a file mapped on hard-disk, the handle to the file m_hFile is NULL;
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the information.
I have prepared my software. When testing on my laptop, it still fails.
However on other PCs, it works well. I could make a shared memory up to 1 GBytes without fail.
Thanks for the help anyway.