| | |
File Mapping Causing Access Violation
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 15
Reputation:
Solved Threads: 3
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?
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...
Note : Error checking and comments removed.
C++ Syntax (Toggle Plain Text)
struct _sSharedArray { std::string strData[ 200 ]; } *sSharedArray = NULL; HANDLE sSeedFileHandle; HANDLE sFileMappingHandle; sSeedFileHandle = CreateFile( "C:\\Seed.file" , GENERIC_READ | GENERIC_WRITE , FILE_SHARE_READ | FILE_SHARE_WRITE , 0 , CREATE_ALWAYS , FILE_ATTRIBUTE_HIDDEN , 0 ); sFileMappingHandle = CreateFileMapping( sSeedFileHandle , 0 , PAGE_READWRITE , 0 , 10000 , "sSharedMap" ); sFileMappingHandle = OpenFileMapping( FILE_MAP_WRITE , 1 , "sSharedMap" ); 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...
C++ Syntax (Toggle Plain Text)
sSharedArray->strData[x] = "This string will cause an access violation!"; sSharedArray->strData[x] = "This will not.";
Note : Error checking and comments removed.
Last edited by Liinker; Dec 23rd, 2008 at 6:07 pm.
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.
•
•
Join Date: Dec 2008
Posts: 15
Reputation:
Solved Threads: 3
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...
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.
C++ Syntax (Toggle Plain Text)
struct _FixedLengthIndex { char cChar[30]; } *pFixedLengthIndex = NULL; struct _sSharedArray { _FixedLengthIndex sIndex[ 200 ]; } *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.
•
•
Join Date: Dec 2008
Posts: 15
Reputation:
Solved Threads: 3
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...
But now when I increase the total number of characters beyond 5000... I get a OutOfMemory exception. Trying to work through that now...
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Dec 2008
Posts: 15
Reputation:
Solved Threads: 3
•
•
•
•
Have you tried different file attributes and access levels, like just all read, no hidden stuff, etc.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: handle to a serial port
- Next Thread: programming explained
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






