I am getting a run-time error in my C++ application and I have tried to track this down in my code using the primitive debugger that they have and have had no luck at all finding this. I am not sure if allocating dynamic memory and then freeing the memory would cause this kind of error. I am not doing any kind of Math operations as the error suggests. I will say I am running a very old version of MS C++ version 1.56 which I am stuck with for the time being. This application runs on a hand-held scanning device and scans barcodes.
/*---- These C functions are used in the Examination of the Array Buffer */
/* and the Serial Numbers being scanned */
/*-----------------------------------------------------------------------*/
/* */
/* Check to see if what was scanned is already in the buffer */
/* */
int isDuplicate(char **myBuff, char *mySERIALtxt, int MaxArraySize)
{
int DuplicateFlag, w;
DuplicateFlag = 0;
for (w=0; w < MaxArraySize; w++)
{
if (strcmp(myBuff[w], mySERIALtxt) == 0)
{
DuplicateFlag = 1;
w = MaxArraySize;
}
}
return DuplicateFlag;
}
/* */
/* If the Serial Number is Deleted on a Recall then re-adjust the array buffer */
/* */
void adjustMyBuffer(char **myBuff, int MaxArraySize)
{
int z;
int adjustflag;
for (z=0; z < MaxArraySize; z++)
{
strcpy(myBuff[z], myBuff[z+1]);
adjustflag = 1;
} /*End For Loop */
} /* End Function */
int ShowForm3(ProgStruct *ps)
{
int tc; /* terminating code */
int rc; /* return code for next form */
int more; /* loop variable */
int hasPrefix; /* Prefix Check for Serial Number*/
int i, x, k, y=0, r=1; /* loop variables */
int isDataGood;
int myBufferSize = 10; /* Set Buffer to 5 for testing */
int myFieldSize = 12;
int BufferCount;
int CurrentPosition;
char SERIALtxt[12];
char NewSerial[12];
char strDate[9];
char strTime[5];
char **myBuffer;
FILE *chan;
more = 1;
BufferCount = 0;
fmClearFlds(Form3); /* clear text boxes */
while (more)
{
if (ps->ReturnToMain) /* return to main menu */
return tc;
tc = fmProcForm(Form3);
switch (tc)
{
case fmF1: /* Recall and Display Last Bufferred Serial Number */
if (x >= 0 && BufferCount >0)
{
x--;
fmSetFld(Form3,"SERIALtxt", myBuffer[x]);
fmSetFocus(Form3,"SERIALtxt");
}
break;
case fmF3: /*F3-Delete pressed - Remove from the Array and re-adjust the buffer */
fmGetFld(Form3,"SERIALtxt",SERIALtxt);
if (strcmp(SERIALtxt, "") != 0 && BufferCount >0)
{
fmClearFlds(Form3);
strcpy(SERIALtxt, "");
strcpy(myBuffer[x], SERIALtxt);
fmSetFld(Form3,"SERIALtxt", myBuffer[x]);
fmSetFocus(Form3,"SERIALtxt");
adjustMyBuffer(myBuffer,myBufferSize);
BufferCount--;
y--;
}else{
fmBuzz();
fmMsg("Error","Invalid","Serial Number");
tc = fmProcForm(Form3);
}
fmClearFlds(Form3);
r = 1;
fmSetFocus(Form3,"SERIALtxt");
break;
case fmF4:
break;
case fmMode: /*ESC Exit pressed - Write out the remaining items in the Buffer */
chan=fopen("BTDATA.TXT", "a");
while (strcmp(myBuffer[0],"") != 0 && strcmp(myBuffer[0], NULL) != 0)
{
if (strcmp(myBuffer[0], "") != 0 && strcmp(myBuffer[0], NULL) != 0)
{
char outbuff[128];
sprintf(outbuff, "%s,%s,%s,%s,%s,%s,%s\n", ps->a,ps->b,ps->c,ps->d,strDate,strTime,myBuffer[0]);
fwrite(outbuff, sizeof(char), strlen(outbuff), chan);
strcpy(myBuffer[0], "");
adjustMyBuffer(myBuffer,myBufferSize);
}
}
for (i = 0; i < myBufferSize; i++)
{
free(myBuffer[i]); /* Free the buffer memory allocated */
}
free(myBuffer); /* Free the character pointer allocated */
fclose(chan);
BufferCount=0;
break;
more = 0;
ShowForm1(ps); /* clear text boxes */
case fmEnter: /*Enter or Scan was executed - Move into Buffer */
fmGetFld(Form3,"SERIALtxt",SERIALtxt);
if(r == 0)
r = 1;
if (isDataGood && tc != 5)
{
if (!(isDuplicate(myBuffer, NewSerial, myBufferSize)))
{
if (SERIALtxt != "" || SERIALtxt != NULL)
{
if (BufferCount == myBufferSize) /* check to see if buffer is full before adding more serial numbers */
{
char outbuff[128];
sprintf(outbuff, "%s,%s,%s,%s,%s,%s,%s\n", ps->a,ps->b,ps->c,ps->d,strDate,strTime,myBuffer[0]);
chan=fopen("BTDATA.TXT", "a");
fwrite(outbuff, sizeof(char), strlen(outbuff), chan);
fclose(chan);
CurrentPosition--;
BufferCount--;
strcpy(myBuffer[0], ""); /* Put "" in its place so it knows where to adjust from */
adjustMyBuffer(myBuffer,myBufferSize);
y--; /* position back to the beginning of the array */
}