Home > Back-end >  Read a file, memory mapping file different random errors
Read a file, memory mapping file different random errors

Time:09-22

This question bothers me more than a month, the ball ball bosses to join to help me look at it,
The calculation results of the purpose of this program is to read in Fluent and the grid into memory, the beginning with the document flow, see I don't think it is too slow evening said can use memory mapping,
The question now is whether the memory mapping code occasionally an error, I find their own position for a long time finally to the following code:

` ` `
 # include & lt; Afx. H> 
# Include & lt; String>
# Include & lt; Vector>
# Include & lt; Iostream>
using namespace std;

//from the Internet to find the string of 2 LPCWSTR type conversion
LPCWSTR stringtoLPCWSTR (string orig)
{
Size_t origsize=orig. Length () + 1;
Size_t convertedChars=0;
Would be * wcstring=(*) would be malloc (sizeof (would) * orig. The size () - 1);
Mbstowcs_s (& amp; ConvertedChars wcstring, origsize, orig. C_str (), _TRUNCATE);
Return wcstring;

}

Void main ()
{
HANDLE fileHandle=NULL, fileMapHandle=NULL;
LPCWSTR=nameString=stringtoLPCWSTR (" grid \ \ NACA0012 cas ");

//create the file object, from the Internet to learn
FileHandle=CreateFile (nameString,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONTLY,
NULL);
If (fileHandle==INVALID_HANDLE_VALUE)
{
//error
}

//triggers the memory conflict code
//this code is simplified, and the content of the variable is consistent with the
//and omit some has ruled out no problem statement
//! Finally, can trigger a memory leak location is after create the file object

//record to find the content of the text lines
Vector LineRecorder;
The contents of the//to get from the text in each row, here with a fixed string instead of
String currentLine="asdasdasdasdasdasdasd";
//loop was used to simulate the process of reading text
for(int i=0; i<100; I++)
{
//if (some)
//is omitted judgment

//the location of the memory strife
//if there is a conflict, memory cycle usually run to the ninth, namely the cout output below 8/100
LineRecorder. Push_back (I);

cout}
getchar();
}

//also omitted behind

}

` ` `
Error message:
Event name: APPCRASH
Failure module: NTDLL. DLL
Exception code: c0000005
Abnormal migration: 000000000002 a1c5

CodePudding user response:

Can track where the crash debugged, a line, or to provide the dump file

CodePudding user response:

The function to write wrong, should be
LPCWSTR stringtoLPCWSTR (string orig)
{
Size_t origsize=orig. Length () + 1;
Size_t convertedChars=0;
Would be * wcstring=(*) would be malloc (sizeof (would) * origsize);
Mbstowcs_s (& amp; ConvertedChars wcstring, origsize, orig. C_str (), _TRUNCATE);
Return wcstring;

}
Or it will randomly crash, because can lead to stack is destroyed

CodePudding user response:


L "grid \ \ NACA0012. Cas";
_T (" grid \ \ NACA0012 cas ");

CodePudding user response:

 # include & lt; Afx. H> 
#include //-- -- -- -- -- -- -- to lowercase I -- -- -- -- -- -- -- -- --
#include //-- -- -- -- -- -- -- to lowercase I -- -- -- -- -- -- -- -- --
#include //-- -- -- -- -- -- -- to lowercase I -- -- -- -- -- -- -- -- --
using namespace std;

LPCWSTR stringtoLPCWSTR (string orig)
{
Size_t origsize=orig. Length () + 1;
Size_t convertedChars=0;
Would be * wcstring=(*) would be malloc (sizeof (would) * orig. The size () - 1);
Mbstowcs_s (& amp; ConvertedChars wcstring, origsize, orig. C_str (), _TRUNCATE);
Return wcstring;

}
Void main ()
{
HANDLE fileHandle=NULL, fileMapHandle=NULL;
LPCWSTR nameString=stringtoLPCWSTR (" grid \ \ NACA0012 cas ");//-- get rid of the equal sign -- -- -- -- -- -- --

//create the file object, from the Internet to learn
FileHandle=CreateFile (nameString,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY,//- to FILE_ATTRIBUTE_READONLY -
NULL);
If (fileHandle==INVALID_HANDLE_VALUE)
{
//error
}

//triggers the memory conflict code
//this code is simplified, and the content of the variable is consistent with the
//and omit some has ruled out no problem statement
//! Finally, can trigger a memory leak location is after create the file object

//record to find the content of the text lines
Vector LineRecorder;
The contents of the//to get from the text in each row, here with a fixed string instead of
String currentLine="asdasdasdasdasdasdasd";
//loop was used to simulate the process of reading text
for(int i=0; i<100; I++)
{
//if (some)
//is omitted judgment

//the location of the memory strife
//if there is a conflict, memory cycle usually run to the ninth, namely the cout output below 8/100
LineRecorder. Push_back (I);

cout}
getchar();
}


VS2015 support MFC debug console
  • Related