Skip to content

Instantly share code, notes, and snippets.

@ElectroLund
Last active August 14, 2019 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectroLund/87d0d498b7233ac613ff4b3d91852e60 to your computer and use it in GitHub Desktop.
Save ElectroLund/87d0d498b7233ac613ff4b3d91852e60 to your computer and use it in GitHub Desktop.
Data type declaration bug on the LabWindows CVI platform.
int timerHandle = 0;
char statusStr[STATUS_MAX_MSG_CHARS];
double servoDuty[3] = {0.98, 2.102, 7.984};
int CVICALLBACK ServoAutoCalibrate (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
int position;
int timerInterval, timerState;
switch (event)
{
case EVENT_COMMIT:
// look up which aperture is being cal'd
GetCtrlVal(panel, PANEL_POSITION, &position);
GetAsyncTimerAttribute(timerHandle, ASYNC_ATTR_INTERVAL, &timerInterval);
for (int k=0; k < CAL_AVERAGE_SIZE * timerInterval; k++)
{
GetAsyncTimerAttribute(timerHandle, ASYNC_ATTR_ENABLED, &timerState);
if (timerState == OFF)
break;
// delay a bit more than the timer period
DelayWithEventProcessing(timerInterval * 1.01);
}
sprintf(statusStr, "Old value = %1.3f pwm", servoDuty[position]);
MessagePopup("Status", statusStr);
break;
}
return 0;
}
@ElectroLund
Copy link
Author

I ran into a code bug that took a while to figure out and I thought I'd pass along the story to warn others.

The following pseudo code contains the bug. The symptom was that mid-execution, the value of position was getting corrupted and thus causing a GPF at the sprintf.

See the bug?

I had mistakenly declared timerInterval as an int, rather than a double. And because I'm not in the habit of error-checking the CVI standard library, the call to GetAsyncTimerAttribute blindly overfilled timerInterval with its double value, which then overwrote the value of position.

position was the collateral damage because it's declared before timerInterval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment