Game (unity3d) with phidgets hangs on close

Supporting Visual Studio on Windows
Post Reply
User avatar
Patrick
Lead Developer
Posts: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by Patrick »

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.

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);
}
-Patrick
thylaxene
Phidgetsian
Posts: 13
Joined: Tue Sep 22, 2015 7:37 pm
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by thylaxene »

Thank you for the fixes and clarification! I look forward to the release.

Cheers.
thylaxene
Phidgetsian
Posts: 13
Joined: Tue Sep 22, 2015 7:37 pm
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by thylaxene »

Which version of the DLL are you using from .NET? I'm getting instant Unity crash as soon as I call into the DLL. Like to make sure I'm using the right one.

Cheers.
daves0
Phidgetsian
Posts: 9
Joined: Fri Feb 15, 2019 11:28 am
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by daves0 »

I'm using the .Net 2.0 dll and the C++ x64 library in Unity 5.6.3 on Linux and it's working well so far. I haven't had the chance to fully test the issues as I'm deep in the middle of something else, but hope to verify the fixes asap.

Thanks, Brian and Patrick, for your work on this!
daves0
Phidgetsian
Posts: 9
Joined: Fri Feb 15, 2019 11:28 am
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by daves0 »

I'm happy to confirm that all issues I've run into have been resolved with this new release. Thanks much to everyone involved!
thylaxene
Phidgetsian
Posts: 13
Joined: Tue Sep 22, 2015 7:37 pm
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by thylaxene »

Unfortunately I'm having no luck using the latest .NET 4.6 DLL with Unity 2018.3 under macOS using Mono 4.xx Equivalent. I've installed the latest drivers and tried several different .NET DLLs and I either get an instant crash or spinning beach ball of death when I call into the plugin.

Cheers.
User avatar
Patrick
Lead Developer
Posts: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by Patrick »

I'm seeing the crash on macOS. Looking into it now.

-Patrick
User avatar
Patrick
Lead Developer
Posts: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by Patrick »

The crash on macOS is a bug when ResetLibrary() is called before anything else - fixed in the next release.

Testing on macOS, it seems that Unity/Mono behave differently with respect to the external assembly, and deadlock waiting for all threads to exit before running the garbage collector.

I'd recommend calling ResetLibrary in OnApplicationQuit() rather then in Start():

Code: Select all

void OnApplicationQuit() {
   jump.Close();
   jump = null;
   if (Application.isEditor)
      Phidget.ResetLibrary();
   else
      Phidget.FinalizeLibrary(0);
}
With this change, I'm able to start/stop the Unity Editor without issues on macOS. This will also work properly on Windows/Linux. I'll update my previous post to reflect this.

-Patrick
thylaxene
Phidgetsian
Posts: 13
Joined: Tue Sep 22, 2015 7:37 pm
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by thylaxene »

Yep that worked for me! Thanks for tracking down the work around. I can now say goodbye to my Python hack for testing in the Editor.

Haven't tested a build yet. But fingers crossed.

Cheers.
kiu
Phidgetly
Posts: 24
Joined: Mon Aug 19, 2019 10:28 am
Contact:

Re: Game (unity3d) with phidgets hangs on close

Post by kiu »

Hi,

I'm still encountering this problem, but exclusively on builds.
I'm using a remote phidget though...

System Win10_64bit
Unity 2019.3.3f1
Interfacing a Phidget Spatial via a Wireless Vint Hub5000_0
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests