Game (unity3d) with phidgets hangs on close

Supporting Visual Studio on Windows
daves0
Phidgetsian
Posts: 9
Joined: Fri Feb 15, 2019 11:28 am
Contact:

Re: Game (unity3d) with phidgets hangs on close

Postby daves0 » Fri Feb 15, 2019 11:39 am

This is affecting me too, almost eight years after the first bug report.

If I have a PhidgetSpatial plugged into my computer via USB and create a PhidgetManager in my game and call Open() on the manager, and call Close() on the manager when the game exits ... the game crashes hard. Not calling the Open/Close pair but simply allocating the manager exits just fine.

There are no events added to the manager, and no other Phidget processing done in code at all. The crash only happens in a stand-alone game, it does not happen in the Unity Editor because that never "exits" when you start and stop a game from inside it. Starting/stopping the game within the editor and then exiting the Editor does happen cleanly though. I don't know what the difference is.

This seems like what other comments have suggested, that events are floating around and need to be handled before exiting. Can the PhidgetManager shutdown process clean up or ignore those events so that it doesn't crash?

Any input from the Phidget folks would be welcome.

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

Re: Game (unity3d) with phidgets hangs on close

Postby daves0 » Mon Feb 18, 2019 5:16 pm

I've noticed that the message

terminate called without an active exception

is showing up occasionally when the program crashes. According to

h t t p s : / / stackoverflow . com/questions/7381757/c-terminate-called-without-an-active-exception

this could be caused by erroneous c++ thread handling. The accepted answer highlights a difference between std:: and boost:: thread handling specifically. Perhaps something bad along these lines is happening in the Phidget code? Can someone from Phidget-land please look into this?

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

Re: Game (unity3d) with phidgets hangs on close

Postby thylaxene » Mon Feb 18, 2019 5:40 pm

The Phidget code is certainly using its own thread. Easily seen when trying call non-thread safe code within Unity Editor. So I suspect it is related to improper cleanup after use. Or not releasing the port. Or something. Ideally they need to open source the DLL. So we can at least have a go at fixing it ourselves as zero support now with Phidgets by the look of things.

Anyway I did a hack to work around this. Using Python to do the communicating with the Phidget and Unity calling the Python script via terminal. Not ideal as there is a delay. But does the job.

Cheers.

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

Re: Game (unity3d) with phidgets hangs on close

Postby Patrick » Tue Feb 19, 2019 4:10 pm

This thread is certainly referencing multiple issues, across 2 completely separate library releases.

If this is a threading issue, you likely need to cleanly close all Phidget objects, and then call Phidget.FinalizeLibrary() before exiting - this ensures all memory is freed and all threads have exited.

If that's not working, I will look into this further.

-Patrick

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

Re: Game (unity3d) with phidgets hangs on close

Postby thylaxene » Tue Feb 19, 2019 7:24 pm

OK great thanks for the response. I will try that and report back.

Cheers.

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

Re: Game (unity3d) with phidgets hangs on close

Postby daves0 » Tue Feb 19, 2019 7:42 pm

I've found that I can cause the hard crashes by having events "flowing" when I exit my program. I am calling FinalizeLibrary() but I suspect control is never getting to that point, as I've surrounded it (and much of my exit code) with Debug.Log calls that never appear.

When I run my program, leave my Spatials alone so there is no input from them, and exit .. all is well. When I wiggle my Spatials while exiting, the hard crash occurs. There is no event-driven code on my side, I'm simply reading the Spatials values once per frame, so I don't think it's my code that's choking on events (hopefully).

Is there perhaps a timing issue involving when queued events get freed / cleared?

In the end, I only need to exit my software when the machine it's running on reboots, so I'm using the very valuable information found at http://unity3dtrenches.blogspot.com to force my program to exit regardless of the state that the Phidgets are in. By replacing

Code: Select all

Application.Quit()

with

Code: Select all

System.Diagnostics.Process.GetCurrentProcess().Kill()

my game reboots when necessary. I hope this information helps someone else who is getting stuck at shutdown. Thanks to fireant991 for pointing to that website!

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

Re: Game (unity3d) with phidgets hangs on close

Postby Patrick » Wed Feb 20, 2019 3:59 pm

If you have not registered any event callbacks, then events should not be causing any crashes.

If you do have events registered, and dispose of your owning object without closing the Phidget and waiting for any pending events, then you could have issues.

-Patrick

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

Re: Game (unity3d) with phidgets hangs on close

Postby thylaxene » Wed Feb 20, 2019 4:21 pm

Can we trouble you for an example of best practise, code flow?

For example here:
https://phidgetseducation.edunext.io/courses/course-v1:phidgetseducation+104+2018_T1/courseware/f7820c1a3e8a479c86ae358b31a1bb83/67f997e3a0254f6d86d925393549d651/3?activate_block_id=block-v1%3Aphidgetseducation%2B104%2B2018_T1%2Btype%40vertical%2Bblock%40efd031051643486194aebdd11b6a85a7

Doesn't seem to show proper clean up on app exit. So I followed that as an example to connect to and register events for the RFID Reader.

Cheers.

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

Re: Game (unity3d) with phidgets hangs on close

Postby daves0 » Wed Feb 20, 2019 4:34 pm

The hard crashes appear to be happening when I call the dotnet library's Phidget.Close() function on my Phidget. At that time the logs display the following error:

Code: Select all

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at Phidget22.Accelerometer.OnAccelerationChange (Phidget22.Events.AccelerometerAccelerationChangeEventArgs e) [0x00000] in <filename unknown>:0
  at Phidget22.Accelerometer.nativeAccelerationChangeEvent (IntPtr phid, IntPtr ctx, System.Double[] acceleration, Double timestamp) [0x00000] in <filename unknown>:0 


This error is displayed whether or not I set an onAccelerationChanged handler in my C# code. When this error happens in the Close() call, the next time that I call Open() on the same Phidget object will result in the hard crash. I'm using the 1.0.0.20190130 version of the DotNet 2.0 Phidget libraries on NuGet (but the same thing happens with 20190117)

How can I flush all / wait for all pending Phidget events using the C# interface? That might resolve the issues we're seeing.

This all happens when I am running my game within the Unity editor. I press the Play button, the game runs and Opens the Phidget, then I press the Play button again (it's a toggle) and the game stops. The Editor, however, is still running and it still is holding the Phidget object. When I hit Play again to run the game again, I see the destructors on my object getting called, and the Phidget's Close() routine running and that error displaying. The game then tries to Open() the Phidget again when my object is allocated and initialized, and then the Unity Editor locks hard.

It's a bit of an odd setup, with Unity holding onto the object in the Editor (not destroying it) and then when I allocate the new Phidget it releases the old one.

Thanks for poking at this!

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

Re: Game (unity3d) with phidgets hangs on close

Postby Patrick » Mon Feb 25, 2019 10:30 am

I will look into this, I'm not sure what could be happening.

-Patrick


Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 1 guest