I’m working on the Kinect, and sporadically the Kinect makes Visual Studio Crash.
It’s happening when I call the kinectSensor.Start() method after debugging I found out that the Kinect is already in use in another process.
But no other processes is using the Kinect!
It happened one time on the morning after booting my machine and launching the app.
But most of the time it occurs when I launch the Kinect App (in debugging mode in VisualStudio 2010), I stop the application and after some code modification I re-launch the app and that time it doesn’t work, I’ve got the above exception and after 2sec Visual Studio crashes.
Moreover I check if the Kinect is already running and this is not the case (below the state of the Kinect sensor during debugging):

The weirdest thing above all is that sometimes it works fine for weeks, and after that it makes that thing for two days every two/three launches of the app.
The only solution I found on the subject is from one guy with a very similar problem (the only one on the internet?) : Imad Elayyan
When I launch Process Explorer I found the exact same problem of USB port handling (shown on the photo), a mutant process which is not terminated when we close the app.

But his solution is not really a long term solution: I have to kill manually the mutant and re-start VisualStudio. Sometimes I have to restart VS 20 times on the morning…
Is there a way to properly close that handle of the usb port (in code?) so that when I close the app it terminate that handle and doesn’t makes VS crash anymore?
Any help would be appreciated, it is really puzzling me and it drives me crazy.
Additional information:
i’m using the SDK1.5 + Kinect for Windows but it was also the case with the SDK1.0 + Kinect XBox360
I’ve already try to debug VS with another instance of VS.
Ok guys, thanks for caring. I think I found the source of my problem but I still lack an explanation for the handle port.
Facts:
I have 3 projects in my solution, two classes library:
Kinect.dllwhich create the connection to the device, handle the gesture
recognitionj part and the cursors creation.
KinectControls.dllwhich create controls (button, border, chart 3D…) that can
interact with the cursors. And a test project.
Kinect.dll launch the event of the Kinect + some custom event:
ColorFrameReady,DepthFrameReady,GestureRecognised…Most of the event are internal in the assembly, but some are visible
from outside classes.
Each event registered is unregistered (or so I thought).
I implemented the facade pattern for the Kinect-related
classes. KinectFacade posseses a
KinectStopmethod which close theconnection and unregister events.
Root of the problem:
When the application was shutting down, the
Application.Current.Shutdown()method called KinectStop which stopped the Kinect and the unregistered the handler in the assembly. But in the outside assemblies the unregistration was called after the Kinect closes (even if the unregistration was called onApplication.Current.Shutdown()too) which led to a ‘basic/usual’ error. So that I removed the unregistration.When you do not unregister of the Kinect event and the Kinect closes (
KinectSensor.stop()method called), it (sporadically) keeps the USB handle even if the application is closed and any thread terminated!! And after that: No way to reopen the application (Kinect.Start()crashes) and finally when you try to close Visual studio it crashes (as explained in my question).Solution I’ve found
When
KinectStop()is called I use a timer before closing the Kinect so that every classes in outside assemblies can unregister events.Moreover from now on no ‘normal’ event are available from the outside classes: the façade handles those events and raises FastSmartWeakEvent which are available from the outside world. It solves memory leaks and could also solve my problem in some situation.
Since then not handle USB port is keeped and Visual Studio do not crash anymore.
Summary
I’ve found the roots of my problem and a solution in order to bypass it. I hope this will help anybody with a similar problem.
However I’still don’t understand why closing the application and/or calling
KinectSensor.Stop()method from the SDK does’nt close all events handler/Threads/processes/Port Handle. And why closing visual studio make it crash even if one of the previous is still running?If anyone has a suggestion it will be welcomed?