I found the following code in a Unity3D project using the Kinect v2 which I have taken over. I'm paranoid so I thought I'd check before I delete it. But surely there is no reason for these two lines??
colorFrame.Dispose();
colorFrame = null;
This is C#. It has automatic garbage collection so my understanding is that colorFrame will be disposed when its convenient outside of the if(GetRGB) statement
if (GetRGB)
{
ColorFrame colorFrame = frame.ColorFrameReference.AcquireFrame ();
if (colorFrame != null)
{
colorFrame.CopyConvertedFrameDataToArray (_ColorData, ColorImageFormat.Rgba);
_ColorTexture.LoadRawTextureData (_ColorData);
_ColorTexture.Apply ();
colorFrame.Dispose ();
colorFrame = null;
}
}
It has automatic garbage collection so my understanding is that colorFrame will be disposed when its convenient outside of the if(GetRGB) statement
The object will be cleaned once that GC kicks in (at a non-deterministic time) and sees there is no root to the colorFrame
object. Calling Dispose
on an object usually releases unmanaged resources allocated by that same object, as well as calls GC.SupressFinalize
, which makes any object that has a finalizer de-register from the finalization queue, allowing the GC to clean it up "faster".
I would suggest keeping the call to Dispose
. I would remove the colorFrame = null
call, which is useless.
Better yet, wrap colorFrame
in a using
statement:
if (GetRGB)
{
using (ColorFrame colorFrame = frame.ColorFrameReference.AcquireFrame())
{
if (colorFrame != null)
{
colorFrame.CopyConvertedFrameDataToArray(_ColorData, ColorImageFormat.Rgba);
_ColorTexture.LoadRawTextureData(_ColorData);
_ColorTexture.Apply();
}
}
}