|
-
November 20th, 2007, 03:53 PM
#1
ReadFile crashes FSX
I am a Flight Simulator add-on developer. I work with FS2004 and FSX, the latest version of the sim.
In my code, I want to read the content of a file, so I use ReadFile in synchronous mode, like this:
HANDLE hFile = CreateFile(fullModuleName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
BOOL readResult = ReadFile(hFile, buffer, MaxSize, &nbBytesRead, NULL);
It works perfect in FS2004, and it used to work well in FSX before the SP1. You're not supposed to know FSX, so I tell you the main new feature of the SP1 is that it supports multi-threading to make the simulation work faster on dual-core machines. Since SP1, my code crashes FSX when it reaches the "ReadFile" statement. There is no way to get any error code, it just crashes.
Do you have any clue? I'm helpless...
Thanks for any help.
Eric
-
November 20th, 2007, 05:15 PM
#2
Re: ReadFile crashes FSX
The only way I can think of is that buffer size and MaxSize don´t match... erm... do you check against a valid HANDLE?
- Guido
-
November 20th, 2007, 06:59 PM
#3
Re: ReadFile crashes FSX
 Originally Posted by Rocky651
I am a Flight Simulator add-on developer. I work with FS2004 and FSX, the latest version of the sim.
In my code, I want to read the content of a file, so I use ReadFile in synchronous mode, like this:
HANDLE hFile = CreateFile(fullModuleName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
BOOL readResult = ReadFile(hFile, buffer, MaxSize, &nbBytesRead, NULL);
Is the hFile valid or invalid? What variable type is buffer? Where is it declared and initialized (if necessary)? What is the value of MaxSize? Is buffer greater than or equal to the capacity determined by MaxSize?
It works perfect in FS2004, and it used to work well in FSX before the SP1.
Maybe you were lucky it worked, and now SP1 has exposed an existing bug in your program.
Regards,
Paul McKenzie
-
November 21st, 2007, 05:14 AM
#4
Re: ReadFile crashes FSX
I didn' want to make my post too complex and too heavy, and this is why I didn't write everything. Obviously, I check the validity of the hFile file handler right after CreateFile. MaxSize has an arbitrary value of 999999 whereas the file I read is around 100000, buffer is an array of char. The "ReadMyFile" function that reads the file returns a boolean result: true if everything was fine, false if a problem was encountered. The whole piece of code looks like this:
static const int MaxSize = 999999;
bool ReadMyFile(char* filename)
{
HANDLE hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
retrun false;
char buffer[MaxSize];
DWORD nbBytesRead = 0;
BOOL readResult = ReadFile(hFile, buffer, MaxSize, &nbBytesRead, NULL);
// This part of code is never executed (it crahes on ReadFile)
if (!readResult)
return false;
// Process the buffer
return true;
}
-
November 21st, 2007, 05:28 AM
#5
Re: ReadFile crashes FSX
I noticed you´re using a very large char array on the stack, maybe it´s larger than the stack and corrupts it?
- Guido
-
November 21st, 2007, 07:34 AM
#6
Re: ReadFile crashes FSX
 Originally Posted by Rocky651
I didn' want to make my post too complex and too heavy, and this is why I didn't write everything.
The minimum you should have given us was the values of those parameters. If the function crashes, doesn't it make sense to show us what values you're calling it with?
Code:
static const int MaxSize = 999999;
bool ReadMyFile(char* filename)
{
HANDLE hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
retrun false;
char buffer[MaxSize];
A local array that is 999,999 bytes could blow out the stack easily. The proper way to do this would be to either dynamically allocate the memory using new[]/delete[], or use a container such as std::vector.
Code:
#include <vector>
//...
HANDLE hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
retrun false;
std::vector<char> buffer(MaxSize);
DWORD nbBytesRead = 0;
BOOL readResult = ReadFile(hFile, &buffer[0], MaxSize, &nbBytesRead, NULL);
// Process the buffer
return true;
}
Regards,
Paul McKenzie
-
November 21st, 2007, 02:59 PM
#7
Re: ReadFile crashes FSX
Thank you very much, it now works fine !!
You were right, my buffer was too big and was corrupting the stack. I didn't think about this because the same code was working fine with FS2004 and FSX before SP1. You were 100% right.
Thanks again !!
Eric
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|