StrikeTheCore
Programmer
I have code running that constantly throws an Access Violation on Windows XP. This exception only occurs however after around 10 to 20 thousand times running this function. I can catch the exception but want to get rid of it completely, however cannot figure out where it is coming from. Code is below (str is a simple string parser). Any help is greatly appreciated.
//--------------------------------------------------------
unsigned char *inbuffer;
AnsiString tmp;
struct timeval current;
inbuffer = new unsigned char[Count+2];
myComPort->Read(inbuffer, Count);
str.reset();
try {
if(Form2->mode == TForm2::HEX_MODE && !Form2->BufferDisabled->Checked){
for(int i=0; i<Count; i++){
tmp.sprintf("%0x ", inbuffer);
if(tmp.Length() < 3)
tmp = AnsiString("0") + tmp;
str << tmp.c_str() << " ";
}
if(Form2->ReceiveBuffer.Length() + str.length() < Form2->MaxBufLen){
Form2->ReceiveBuffer += AnsiString(str.c_str()).UpperCase();
}
else{
unsigned int n=0;
for(int count=0;n<=str.length();count++)
n += (*(Form2->Memo->Lines))[count].Length();
Form2->ReceiveBuffer.Delete(0, n);
Form2->ReceiveBuffer += AnsiString(str.c_str()).UpperCase();
}
}
else if(Form2->mode == TForm2::ASCII_MODE && !Form2->BufferDisabled->Checked){
inbuffer[Count] = '\0';
if(Form2->ReceiveBuffer.Length() + strlen((char*)inbuffer) > Form2->MaxBufLen){
Form2->ReceiveBuffer.Delete(0, strlen((char*)inbuffer));
}
Form2->ReceiveBuffer += AnsiString((char*)inbuffer);
if(Form2->logdata){
std:fstream outfile;
outfile.open(Form2->LogFileName.c_str(), std:fstream:ut | std:fstream::app);
outfile << (char*)inbuffer;
outfile.close();
}
}
}
catch (const EAccessViolation & vae)
{
ShowMessage("Misread message");
}
//--------------------------------------------------------
unsigned char *inbuffer;
AnsiString tmp;
struct timeval current;
inbuffer = new unsigned char[Count+2];
myComPort->Read(inbuffer, Count);
str.reset();
try {
if(Form2->mode == TForm2::HEX_MODE && !Form2->BufferDisabled->Checked){
for(int i=0; i<Count; i++){
tmp.sprintf("%0x ", inbuffer);
if(tmp.Length() < 3)
tmp = AnsiString("0") + tmp;
str << tmp.c_str() << " ";
}
if(Form2->ReceiveBuffer.Length() + str.length() < Form2->MaxBufLen){
Form2->ReceiveBuffer += AnsiString(str.c_str()).UpperCase();
}
else{
unsigned int n=0;
for(int count=0;n<=str.length();count++)
n += (*(Form2->Memo->Lines))[count].Length();
Form2->ReceiveBuffer.Delete(0, n);
Form2->ReceiveBuffer += AnsiString(str.c_str()).UpperCase();
}
}
else if(Form2->mode == TForm2::ASCII_MODE && !Form2->BufferDisabled->Checked){
inbuffer[Count] = '\0';
if(Form2->ReceiveBuffer.Length() + strlen((char*)inbuffer) > Form2->MaxBufLen){
Form2->ReceiveBuffer.Delete(0, strlen((char*)inbuffer));
}
Form2->ReceiveBuffer += AnsiString((char*)inbuffer);
if(Form2->logdata){
std:fstream outfile;
outfile.open(Form2->LogFileName.c_str(), std:fstream:ut | std:fstream::app);
outfile << (char*)inbuffer;
outfile.close();
}
}
}
catch (const EAccessViolation & vae)
{
ShowMessage("Misread message");
}