Copilot commented on code in PR #504:
URL: https://github.com/apache/arrow-dotnet/pull/504#discussion_r4793861438


##########
src/Apache.Arrow/Arrays/BinaryViewArray.cs:
##########
@@ -85,9 +125,21 @@ public TArray Build(MemoryAllocator allocator = default)
             {
                 bool hasValues = ValueBuffer.Length > 0;
                 var bufs = new ArrowBuffer[hasValues ? 3 : 2];
-                bufs[0] = NullCount > 0 ? ValidityBuffer.Build(allocator) : 
ArrowBuffer.Empty;
-                bufs[1] = BinaryViews.Build(allocator);
-                if (hasValues) { bufs[2] = ValueBuffer.Build(allocator); }
+                try
+                {
+                    bufs[0] = NullCount > 0 ? ValidityBuffer.Build(allocator) 
: ArrowBuffer.Empty;
+                    bufs[1] = BinaryViews.Build(allocator);
+                    if (hasValues) { bufs[2] = ValueBuffer.Build(allocator); }
+                }

Review Comment:
   The cleanup scope ends before `ArrayData` construction and `Build(data)`, so 
failures there leave built buffers undisposed. For example, building a nonempty 
`BinaryViewArray.Builder` with `Int32Type.Default` throws during type 
validation and leaks caller-owned memory even if the builder is disposed. Keep 
both construction steps inside the `try`, transferring ownership only when the 
array is successfully returned.



##########
src/Apache.Arrow/Arrays/MapArray.cs:
##########
@@ -39,11 +39,25 @@ public class MapArray : ListArray // MapArray = 
ListArray(StructArray("key", "va
             public MapType DataType { get; }
 
             public Builder(MapType type)
+                : this(type, null)
             {
-                KeyBuilder = 
ArrowArrayBuilderFactory.Build(type.KeyField.DataType);
-                ValueBuilder = 
ArrowArrayBuilderFactory.Build(type.ValueField.DataType);
-                ValueOffsetsBufferBuilder = new ArrowBuffer.Builder<int>();
-                ValidityBufferBuilder = new ArrowBuffer.BitmapBuilder();
+            }
+
+            /// <summary>
+            /// Creates a builder whose working memory comes from <paramref 
name="allocator"/>.
+            /// </summary>
+            /// <param name="type">The map type to build.</param>
+            /// <param name="allocator">
+            /// Allocator for the builder's working memory, which usually 
becomes the built array's
+            /// memory. When <c>null</c>, the builder works in managed memory 
and copies
+            /// what it builds into <see cref="MemoryAllocator.Default"/>, as 
builders always have.
+            /// </param>
+            public Builder(MapType type, MemoryAllocator allocator)
+            {
+                KeyBuilder = 
ArrowArrayBuilderFactory.Build(type.KeyField.DataType, allocator);
+                ValueBuilder = 
ArrowArrayBuilderFactory.Build(type.ValueField.DataType, allocator);
+                ValueOffsetsBufferBuilder = new ArrowBuffer.Builder<int>(0, 
allocator);
+                ValidityBufferBuilder = new ArrowBuffer.BitmapBuilder(0, 
allocator);
                 DataType = type;

Review Comment:
   Constructing a map with string keys and unsupported struct values allocates 
the key builder's offsets buffer, then throws while creating the value builder. 
No map builder is returned for disposal, permanently leaking caller-owned 
memory; an allocator refusing the value builder's allocation has the same 
effect. Dispose already-created children when construction fails.



##########
src/Apache.Arrow/Arrays/VariantArray.cs:
##########
@@ -390,6 +390,26 @@ public class Builder
             private static readonly Lazy<(byte[] Metadata, byte[] Value)> 
NullPlaceholder =
                 new Lazy<(byte[], byte[])>(() => new 
VariantBuilder().Encode(VariantValue.Null));
 
+            public Builder()
+                : this(null)
+            {
+            }
+
+            /// <summary>
+            /// Creates a builder whose working memory comes from <paramref 
name="allocator"/>.
+            /// </summary>
+            /// <param name="allocator">
+            /// Allocator for the builder's working memory, which usually 
becomes the built array's
+            /// memory. When <c>null</c>, the builder works in managed memory 
and copies
+            /// what it builds into <see cref="MemoryAllocator.Default"/>, as 
builders always have.
+            /// </param>
+            public Builder(MemoryAllocator allocator)
+            {
+                _metadataBuilder = new BinaryArray.Builder(BinaryType.Default, 
allocator);
+                _valueBuilder = new BinaryArray.Builder(BinaryType.Default, 
allocator);
+                _validityBuilder = new ArrowBuffer.BitmapBuilder(0, allocator);

Review Comment:
   Each binary child eagerly allocates its initial offsets buffer, so an 
allocator refusing the second allocation makes this constructor throw after 
`_metadataBuilder` has allocated. No variant builder is returned for disposal, 
permanently leaking caller-owned memory. Dispose already-created children 
before rethrowing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to