Rapid calls to fread crashes the application
- by Slynk
I'm writing a function to load a wave file and, in the process, split the data into 2 separate buffers if it's stereo. The program gets to i = 18 and crashes during the left channel fread pass. (You can ignore the couts, they are just there for debugging.) Maybe I should load the file in one pass and use memmove to fill the buffers?
if(params.channels == 2){
params.leftChannelData = new unsigned char[params.dataSize/2];
params.rightChannelData = new unsigned char[params.dataSize/2];
bool isLeft = true;
int offset = 0;
const int stride = sizeof(BYTE) * (params.bitsPerSample/8);
for(int i = 0; i < params.dataSize; i += stride)
{
std::cout << "i = " << i << " ";
if(isLeft){
std::cout << "Before Left Channel, ";
fread(params.leftChannelData+offset, sizeof(BYTE), stride, file + i);
std::cout << "After Left Channel, ";
}
else{
std::cout << "Before Right Channel, ";
fread(params.rightChannelData+offset, sizeof(BYTE), stride, file + i);
std::cout << "After Right Channel, ";
offset += stride;
std::cout << "After offset incr.\n";
}
isLeft != isLeft;
}
} else {
params.leftChannelData = new unsigned char[params.dataSize];
fread(params.leftChannelData, sizeof(BYTE), params.dataSize, file);
}