This is an automated email from the ASF dual-hosted git repository.

CurtHagenlocher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-dotnet.git


The following commit(s) were added to refs/heads/main by this push:
     new 45b0070  fix: Don't throw when releasing an exported NullArray (#469)
45b0070 is described below

commit 45b0070c479edb76850bb69e309ed9ce2a894908
Author: Curt Hagenlocher <[email protected]>
AuthorDate: Sun Oct 18 09:46:32 2026 -0700

    fix: Don't throw when releasing an exported NullArray (#469)
    
    ## What's Changed
    
    Exporting a `NullArray` with `CArrowArrayExporter.ExportArray` and then
    releasing it throws `ArgumentOutOfRangeException: bytesAllocated ('0')
    must be a non-negative and non-zero value`. This happens for any length.
    
    A `NullArray` has no buffers and no children, so the export allocates
    nothing through `ExportedAllocationOwner`. On release, `Dispose()`
    called `GC.RemoveMemoryPressure(_allocationSize)` with a size of 0, and
    the runtime rejects that. Every other array type allocates at least its
    buffer-pointer array, which is why this hasn't come up before.
    `Dispose()` now only removes memory pressure when something was
    allocated.
    
    The release runs inside the C Data release callback. When a native
    consumer calls that callback, the exception would cross the native
    boundary.
    
    ### Tests
    `RoundTripNullArray` exports, imports and releases null arrays of length
    0 and 3. It fails without the fix on net8.0, net462 and net472.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 src/Apache.Arrow/Memory/ExportedAllocationOwner.cs |  7 ++++++-
 test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/Apache.Arrow/Memory/ExportedAllocationOwner.cs 
b/src/Apache.Arrow/Memory/ExportedAllocationOwner.cs
index 0db1df9..63d0ce1 100644
--- a/src/Apache.Arrow/Memory/ExportedAllocationOwner.cs
+++ b/src/Apache.Arrow/Memory/ExportedAllocationOwner.cs
@@ -103,7 +103,12 @@ namespace Apache.Arrow.Memory
                 _sharedHandles[i] = default;
             }
 
-            GC.RemoveMemoryPressure(_allocationSize);
+            // Nothing is allocated when exporting an array without buffers or 
children (e.g. a NullArray),
+            // and RemoveMemoryPressure throws for zero.
+            if (_allocationSize > 0)
+            {
+                GC.RemoveMemoryPressure(_allocationSize);
+            }
             GC.SuppressFinalize(this);
             _disposed = true;
         }
diff --git a/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs 
b/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs
index 2823df0..1936359 100644
--- a/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs
+++ b/test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs
@@ -111,6 +111,24 @@ namespace Apache.Arrow.Tests
             CArrowArray.Free(cArray);
         }
 
+        [Theory]
+        [InlineData(0)]
+        [InlineData(3)]
+        public unsafe void RoundTripNullArray(int length)
+        {
+            // A NullArray has no buffers or children, so exporting it 
allocates nothing.
+            var array = new NullArray(length);
+            CArrowArray* cArray = CArrowArray.Create();
+            CArrowArrayExporter.ExportArray(array, cArray);
+            using (var imported = 
(NullArray)CArrowArrayImporter.ImportArray(cArray, array.Data.DataType))
+            {
+                Assert.Equal(length, imported.Length);
+                Assert.Equal(length, imported.NullCount);
+            }
+            Assert.True(cArray->release == default);
+            CArrowArray.Free(cArray);
+        }
+
         [Fact]
         public unsafe void ExportArrayPreservesSource()
         {

Reply via email to