Anselme
Anselme

Reputation: 11

Kinect v2 FrameArrived event suddenly stops being fired : am I leaking a IDisposable?

I'm facing a bug in my C# + Kinect v2 API application.

At rare times, the Kinect2 API event BodyFrameReader.FrameArrived (which informs that a new frame is available) suddenly stops being fired, so my functions subscribed to this event stop being called, having catastrophic outcomes on the whole application.

It's a really hard to reproduce bug, because it only happens when there are more than 3-4 people facing the sensor (which I cannot get on the go) : I absolutely have no idea how this factor can be realted to my bug, however, I cannot reproduce it on my own, I always need 3-4 people. The only thing sure is that either the event BodyFrameReader.FrameArrived stops being fired at the source, or that my subscribed methods are strangely unsubscribed to this event (although I don't think that's possible).

I'm trying to figure out if it's a bug of the API / driver, or if my code is responsible for this. I found a video of a Microsoft developper explaining that this kind of problem usually happens when you leak a FrameReference, i.e. forget to call Dispose() on it. But I do call Dispose() on my frame and I couldn't see how I could leak a frame (see code below).

I know I should be using the **using ** keyword and I will : I just want to be sure if that will really solve my problem, as I won't be able to verify that the problem is solved. Also I verified that my methods between the 2 commented lines are not generating any exceptions or error that would prevent the frame.Dispose(); from being called after.

Thanks for the help

private static void onBodyFrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
    var frame = e.FrameReference.AcquireFrame();

    if (frame != null)
    {
        var floorClipPlane = frame.FloorClipPlane;
        
        m_captorAngle = System.Math.Atan(floorClipPlane.Z / floorClipPlane.Y);
        m_captorHeight = floorClipPlane.W;

        if (m_bodies == null)
            m_bodies = new Body[m_kinectSensor.BodyFrameSource.BodyCount];
        
        //BELOW SOME APPLICATION SPECIFIC FUNCTIONS
        gatherBodies();
        sortBodies();
        affectBodiesToPlayers();
        //-----------------------------------------

        frame.GetAndRefreshBodyData(m_bodies);
        frame.Dispose();
        frame = null;
    }
}

Upvotes: 1

Views: 41

Answers (0)

Related Questions