Hello,

I have a very weird problem. I have been using ReadFile() for reading data from a device connected to the PC over USB. As the data to be read is received in chunks of 64 bytes, always, ReadFile() is called with 64 bytes to read. However, when i have to read a frame less than 64 bytes, occasionally a crash occurs.

I shall give an example of the code written:

//Memory allocation done to read 100 bytes from device - This value is known in prior and hence memory is allocated exactly
new(100bytes);
// First read of 64 bytes
ReadFile(64)
// Second read of 64 bytes
ReadFile(64)

-> Here data that would be sent from the device is only 100-64 = 36. Hence, even if I say a read for 64 bytes, there would be no scenario where data greater than 36bytes. hence, this code should work (though i know it is incorrect to allocate less memory and call ReadFile for size greater than the memory allocation).

Question: Could ReadFile() crash if data read is done for more bytes than memory allocation done (though we know what is the exact size of data i need to read)

PS: The reason for such kind of an implementation was a small flaw in design of the code that was developed.

Well the answer would begin by you producing a debug build, then running the code in a debugger (say WinDbg), and from there finding out some actual usable information about the crash.

As opposed to merely speculating about where the problem might be with little or no evidence (at least none you bothered to tell us about anyway).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.