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.
DBTechniqueAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pgnatyukCommented:
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".
DBTechniqueAuthor Commented:
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

jkrCommented:
How are you initializing 'stSize'?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

pgnatyukCommented:
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.
DBTechniqueAuthor Commented:
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 ?
pgnatyukCommented:
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?
 
 
DBTechniqueAuthor Commented:
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").
DBTechniqueAuthor Commented:
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;
pgnatyukCommented:
>> m_hFile is NUL
?
Can you make a separate small program that will fully use the code from the link I posted?

Pay attention of the following code:
 hMapFile = CreateFileMapping(
                 INVALID_HANDLE_VALUE,    // use paging file
                 NULL,                                   // default security
                 PAGE_READWRITE,              // read/write access
                 0,                                        // maximum object size (high-order DWORD)
                 BUF_SIZE,                          // maximum object size (low-order DWORD)  
                 szName);                            // name of mapping object

Do you see INVALID_HANDLE_VALUE? It is not NULL. You can say that this is -1.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DBTechniqueAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.