File Mapping Causing Access Violation

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 15
Reputation: Liinker is an unknown quantity at this point 
Solved Threads: 3
Liinker Liinker is offline Offline
Newbie Poster

File Mapping Causing Access Violation

 
0
  #1
Dec 23rd, 2008
I'm mapping a file view to share an array of 200 std::strings between two processes but I've been getting an access violation error when I write a string longer than 15 characters into the array. Does anyone know how I might increase the size of each array element so it doesn't overflow and cause problems?

  1.  
  2. struct _sSharedArray {
  3. std::string strData[ 200 ];
  4. } *sSharedArray = NULL;
  5.  
  6. HANDLE sSeedFileHandle;
  7.  
  8. HANDLE sFileMappingHandle;
  9.  
  10. sSeedFileHandle = CreateFile( "C:\\Seed.file" , GENERIC_READ | GENERIC_WRITE , FILE_SHARE_READ | FILE_SHARE_WRITE , 0 , CREATE_ALWAYS , FILE_ATTRIBUTE_HIDDEN , 0 );
  11.  
  12. sFileMappingHandle = CreateFileMapping( sSeedFileHandle , 0 , PAGE_READWRITE , 0 , 10000 , "sSharedMap" );
  13.  
  14. sFileMappingHandle = OpenFileMapping( FILE_MAP_WRITE , 1 , "sSharedMap" );
  15.  
  16. sSharedArray = ( struct _sSharedArray* ) MapViewOfFile( sFileMappingHandle , FILE_MAP_WRITE , 0 , 0 , 0 );

MapViewOfFile returns the starting address of the mapped view so I set that to the starting address of the array.

Now if I write a string into the array...

  1.  
  2. sSharedArray->strData[x] = "This string will cause an access violation!";
  3.  
  4. sSharedArray->strData[x] = "This will not.";

Note : Error checking and comments removed.
Last edited by Liinker; Dec 23rd, 2008 at 6:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File Mapping Causing Access Violation

 
0
  #2
Dec 23rd, 2008
I have not actually worked with file mapping, but I suspect the problem is that std::string attempts to allocate memory which causes the file mapping to crash. Try replacing std::string strData[ 200 ]; with character arrays char strData[200][255] and see if that fixes the problem.
Last edited by Ancient Dragon; Dec 23rd, 2008 at 11:22 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 15
Reputation: Liinker is an unknown quantity at this point 
Solved Threads: 3
Liinker Liinker is offline Offline
Newbie Poster

Re: File Mapping Causing Access Violation

 
0
  #3
Dec 24th, 2008
After about an hour of not receiving any responses yesterday, I decided to do exactly that. Although, slightly different. I changed the array definition to...

  1.  
  2. struct _FixedLengthIndex {
  3. char cChar[30];
  4. } *pFixedLengthIndex = NULL;
  5.  
  6. struct _sSharedArray {
  7. _FixedLengthIndex sIndex[ 200 ];
  8. } *pSharedArray = NULL;

Basically the same thing. I still get an access violation but now i can write a 24 character string. Any more causes the error. I also increased the string length to 50 chars... but it still limited me to 24 before causing the error. I'm going to try your multi-dimensional array and report back. Thanks for the suggestion.
Last edited by Liinker; Dec 24th, 2008 at 10:35 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 15
Reputation: Liinker is an unknown quantity at this point 
Solved Threads: 3
Liinker Liinker is offline Offline
Newbie Poster

Re: File Mapping Causing Access Violation

 
0
  #4
Dec 24th, 2008
I tried your multi-dimension suggestion and I still have the 24 character limit. I'm scratching my head here... I guess I'll keep trying different things.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 15
Reputation: Liinker is an unknown quantity at this point 
Solved Threads: 3
Liinker Liinker is offline Offline
Newbie Poster

Re: File Mapping Causing Access Violation

 
0
  #5
Dec 24th, 2008
Well I solved the access violation error by iterating through the array when its first initialized and writing an 'x' to each char. I'm guessing this allocated the correct amount of memory while the dll was being loaded rather than after the additional modules were loaded.

But now when I increase the total number of characters beyond 5000... I get a OutOfMemory exception. Trying to work through that now...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 977
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: File Mapping Causing Access Violation

 
0
  #6
Dec 24th, 2008
Have you tried different file attributes and access levels, like just all read, no hidden stuff, etc.
Last edited by MosaicFuneral; Dec 24th, 2008 at 4:55 pm.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 15
Reputation: Liinker is an unknown quantity at this point 
Solved Threads: 3
Liinker Liinker is offline Offline
Newbie Poster

Re: File Mapping Causing Access Violation

 
0
  #7
Dec 26th, 2008
Originally Posted by MosaicFuneral View Post
Have you tried different file attributes and access levels, like just all read, no hidden stuff, etc.
Unfortunately both processes need to read and write to the mapped file, although at different times, and the hidden attribute was just recently added for cosmetic reasons. I get the same error regardless.

I'm thinking that because the mapped file is inside the memory space for each process, I need to somehow increase the memory space size to accommodate for the file.

Thats really my only lead right now.
Last edited by Liinker; Dec 26th, 2008 at 12:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 15
Reputation: Liinker is an unknown quantity at this point 
Solved Threads: 3
Liinker Liinker is offline Offline
Newbie Poster

Re: File Mapping Causing Access Violation

 
0
  #8
Dec 29th, 2008
Problem solved. I must have been exceeding the allocated memory space for my application. I actually was mapping (4) 10k files into the space but was only using one of them. After removing the unused 3 files, I was able to increase the index lengths to 100 characters.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC