Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException in LargeArrayPool.cs line 36 #754

Closed
bastiankayser opened this issue Sep 11, 2019 · 3 comments · Fixed by #1006
Closed

NullReferenceException in LargeArrayPool.cs line 36 #754

bastiankayser opened this issue Sep 11, 2019 · 3 comments · Fixed by #1006
Assignees
Labels
Bug Something isn't working CSharp This issue affects only the C# Wrapper Triage Approved The Issue has been approved by an Azure Kinect team member.

Comments

@bastiankayser
Copy link

Describe the bug

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.Azure.Kinect.Sensor
  StackTrace:
   at Microsoft.Azure.Kinect.Sensor.LargeArrayPool.Rent(Int32 minimumLength) in D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\csharp\SDK\LargeArrayPool.cs:line 36
   at Microsoft.Azure.Kinect.Sensor.Allocator.AllocateFunction(Int32 size, IntPtr& context) in D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\csharp\SDK\Allocator.cs:line 317

To Reproduce
I get depth images from the sensor and use a transform to create the point cloud from the depth image. The code looks like the following:

using (Capture capture = AzureKinect.GetCapture())
{
	var targetArray = new Int16[depthImage.HeightPixels * depthImage.WidthPixels*3];
	var pointCloudImage = transform.DepthImageToPointCloud(capture.Depth, CalibrationDeviceType.Depth);
	
	unsafe
	{
		using (var pin = pointCloudImage.Memory.Pin())
		{
			fixed (void* targetArrayPointer = &targetArray[0])
			{
				Buffer.MemoryCopy(pin.Pointer, targetArrayPointer, targetArray.Length*2,
					pointCloudImage.Size);
				
	
			}
	
		}
	
	}
	captureDepth.Dispose();
}

This works for some time but eventually throws the NullReferenceException.

Expected behavior
No NullReferenceException

Desktop (please complete the following information):

  • OS with Version: Windows 10 Pro Build 18363
  • SDK Version: 1.2.0 (nuget package
  • Firmware version:
    Current Firmware Versions:
    RGB camera firmware: 1.6.102
    Depth camera firmware: 1.6.75
    Depth config file: 6109.7
    Audio firmware: 1.6.14
    Build Config: Production
    Certificate Type: Microsoft
@bastiankayser bastiankayser added Bug Something isn't working Triage Needed The Issue still needs to be reviewed by Azure Kinect team members. labels Sep 11, 2019
@tesych tesych added CSharp This issue affects only the C# Wrapper and removed Triage Needed The Issue still needs to be reviewed by Azure Kinect team members. labels Sep 16, 2019
@ahoink
Copy link

ahoink commented Oct 29, 2019

Happens to me too, but only on rare occasions. I'm using SDK version 1.3.0 from the nuget package. Camera firmware is all the same as the original post.

@CameronVetter
Copy link

Same here, on rare occassion I end up in this code:

                if (x.IsAlive)
                {
                    buffer = (byte[])x.Target;
                    if (buffer.Length >= minimumLength)
                    {
                        _ = this.pool.Remove(x);
                        return buffer;
                    }
                }

x.isAlive will be true for the if condition but by the time the buffer assignment happens isAlive is false and Target is null. Seems like there is a thread safety issue here.

@tesych tesych added the Triage Approved The Issue has been approved by an Azure Kinect team member. label Dec 6, 2019
@tesych tesych assigned Brent-A and unassigned DerekMa-WP Dec 6, 2019
@robharrisonuk
Copy link
Contributor

robharrisonuk commented Dec 21, 2019

I've just had this problem and it was due to the garbage collector being called on another thread after the x.IsAlive test and before buffer is assigned. My fix was to test if buffer is null before using it (which according to the WeakReference docs is a valid test - also see https://stackoverflow.com/questions/1687045/thread-safety-of-weakreference)

robharrisonuk added a commit to robharrisonuk/Azure-Kinect-Sensor-SDK that referenced this issue Dec 21, 2019
…other thread after the x.IsAlive test and before buffer is assigned. The fix is to test if buffer is null before using it. (refer to https://stackoverflow.com/questions/1687045/thread-safety-of-weakreference)
@robharrisonuk robharrisonuk mentioned this issue Dec 21, 2019
7 tasks
Brent-A pushed a commit that referenced this issue Jan 14, 2020
* Fix for issue #754. Garbage collector can being called on another thread after the x.IsAlive test and before buffer is assigned. The fix is to test if buffer is null before using it. (refer to https://stackoverflow.com/questions/1687045/thread-safety-of-weakreference)

* Fix crash when calling k4a_device_get_installed_count(), libusb_get_device_list() can return a negative number on some machines which would crash usb_cmd_get_device_count().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working CSharp This issue affects only the C# Wrapper Triage Approved The Issue has been approved by an Azure Kinect team member.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants