Re: Game (unity3d) with phidgets hangs on close
Posted: Mon Mar 04, 2019 3:32 pm
I have fixed the issue with lockups in the editor and during close. These were related to events coming in while the garbage collector is cleaning up Phidget class objects. Release will be later today.
With this fix and the following suggestions, I am able to use the editor with Phidgets across many run cycles without any issues, as well as running standalone apps.
It's important to realize that in the editor, Unity/Mono is leaving the Phidget library loaded the entire time, across start/stop cycles. This means that the internal state of the library is maintained. Mono seems to usually do a good job of running the garbage collector on stop, and this will close all Phidgets, so they can be opened again on the next run - but at least sometimes, I have noticed that they are not garbage collected.
This can also cause issues with static Phidget library calls, like Log.enable() - which will fail if logging has already been enabled in a previous run.
In order to deal with this, you can call Phidget.ResetLibrary() when running in the editor. ResetLibrary() must be the last call into the library. Note that this method is only available in .NET as of today's release.
It is very important to call close() on all Phidget object before exiting, especially when not running in the editor, because otherwise the application can hang. You can do this in the OnApplicationQuit().
It is also possible to do a full clean-up of the Phidget library on exit by call Phidget.FinalizeLibrary(0). This should not be generally needed, but may be needed in some cases. It's important to never call this in the Editor, because the library is not unloaded, and calling any methods after FinalizeLibrary() is unsafe.
Before calling FinalizeLibrary() or ResetLibrary() it's necessary to deference all Phidget objects (by setting them to null) - otherwise their Finalizers may crash the library.
-Patrick
With this fix and the following suggestions, I am able to use the editor with Phidgets across many run cycles without any issues, as well as running standalone apps.
It's important to realize that in the editor, Unity/Mono is leaving the Phidget library loaded the entire time, across start/stop cycles. This means that the internal state of the library is maintained. Mono seems to usually do a good job of running the garbage collector on stop, and this will close all Phidgets, so they can be opened again on the next run - but at least sometimes, I have noticed that they are not garbage collected.
This can also cause issues with static Phidget library calls, like Log.enable() - which will fail if logging has already been enabled in a previous run.
In order to deal with this, you can call Phidget.ResetLibrary() when running in the editor. ResetLibrary() must be the last call into the library. Note that this method is only available in .NET as of today's release.
It is very important to call close() on all Phidget object before exiting, especially when not running in the editor, because otherwise the application can hang. You can do this in the OnApplicationQuit().
It is also possible to do a full clean-up of the Phidget library on exit by call Phidget.FinalizeLibrary(0). This should not be generally needed, but may be needed in some cases. It's important to never call this in the Editor, because the library is not unloaded, and calling any methods after FinalizeLibrary() is unsafe.
Before calling FinalizeLibrary() or ResetLibrary() it's necessary to deference all Phidget objects (by setting them to null) - otherwise their Finalizers may crash the library.
Code: Select all
void Start() {
jump = new DigitalInput();
jump.StateChange += onStateChange;
jump.Open();
}
void OnApplicationQuit() {
jump.Close();
jump = null;
if (Application.isEditor)
Phidget.ResetLibrary();
else
Phidget.FinalizeLibrary(0);
}