SBC2: Bug in Phidget22 C library (20190116) Phidget_finalize
Posted: Sun Jan 20, 2019 7:12 am
I've been working on my little application and was noticing on rare occasions mutex exceptions were being thrown, so I did some testing. After a while it was apparent that, calling Phidget_finalize(0); when exiting causes a subsequent execution to occasionally throw the exception shown below.
When Phidget_finalize is removed the exception is not thrown.
I'm guessing that by not using Phidget_finalize the library is actually recycling resources between calls so the mutex is never actually released.
By doing Phidget_finalize the library seems to be releasing the mutex but not marking the resource gone. On the next execution the library tries to recycle the resources but throws the exception as the mutex has gone.
There is sample code below and a full project in the attached zip. To test the code, compile and use the testloop shell script. Remember to chown the script.
Run it for a couple of minutes to see the exception.
Code: Select all
lock 0x40286da0 failed with 22 (Success)
Test2: src/ext/mos/mos_lock-pthread.c:292: mos_mutex_lock: Assertion `res == 0' failed.
I'm guessing that by not using Phidget_finalize the library is actually recycling resources between calls so the mutex is never actually released.
By doing Phidget_finalize the library seems to be releasing the mutex but not marking the resource gone. On the next execution the library tries to recycle the resources but throws the exception as the mutex has gone.
There is sample code below and a full project in the attached zip. To test the code, compile and use the testloop shell script. Remember to chown the script.
Run it for a couple of minutes to see the exception.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <phidget22.h>
#include "PhidgetHelperFunctions.h"
int main() {
PhidgetVoltageRatioInputHandle ch = NULL;
PhidgetReturnCode prc;
double sensorValue;
prc = PhidgetVoltageRatioInput_create(&ch);
CheckError(prc, "Creating Channel", (PhidgetHandle *)&ch);
prc = Phidget_setDeviceSerialNumber((PhidgetHandle)ch, -1);
CheckError(prc, "Setting DeviceSerialNumber", (PhidgetHandle *)&ch);
prc = Phidget_setHubPort((PhidgetHandle)ch, -1);
CheckError(prc, "Setting HubPort", (PhidgetHandle *)&ch);
prc = Phidget_setIsHubPortDevice((PhidgetHandle)ch, 0);
CheckError(prc, "Setting IsHubPortDevice", (PhidgetHandle *)&ch);
prc = Phidget_setChannel((PhidgetHandle)ch, 0);
CheckError(prc, "Setting Channel", (PhidgetHandle *)&ch);
prc = Phidget_openWaitForAttachment((PhidgetHandle)ch, 5000);
CheckOpenError(prc, (PhidgetHandle *)&ch);
Sleep(100);
PhidgetVoltageRatioInput_getSensorValue(ch, &sensorValue);
prc = Phidget_close((PhidgetHandle)ch);
CheckError(prc, "Closing Channel", (PhidgetHandle *)&ch);
prc = PhidgetVoltageRatioInput_delete(&ch);
CheckError(prc, "Deleting Channel", (PhidgetHandle *)&ch);
// if the following line is removed no exception is thrown
Phidget_finalize(0);
Sleep(1000);
return 0;
}