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


##########
src/Apache.Arrow/Ipc/ArrowReaderImplementation.cs:
##########
@@ -168,6 +167,35 @@ internal static ByteBuffer 
CreateByteBuffer(ReadOnlyMemory<byte> buffer)
             return new ByteBuffer(new ReadOnlyMemoryBufferAllocator(buffer), 
0);
         }
 
+        /// <summary>
+        /// Builds a record batch whose buffers hold references to the message 
body, so that anything
+        /// that shares them, such as a slice, a retained array or an exported 
array, keeps the body
+        /// alive after the batch is disposed. The batch holds no reference of 
its own: the body is
+        /// released along with the last buffer that refers to it.
+        /// </summary>
+        /// <param name="memoryOwner">
+        /// The message body, of which this takes ownership, including on 
failure.
+        /// </param>
+        private RecordBatch ReadRecordBatch(
+            MetadataVersion version,
+            Flatbuf.RecordBatch recordBatch,
+            ByteBuffer bodyByteBuffer,
+            IMemoryOwner<byte> memoryOwner)
+        {
+            MessageBody body = memoryOwner == null ? null : new 
MessageBody(memoryOwner);
+            try
+            {
+                List<IArrowArray> arrays = BuildArrays(version, Schema, 
bodyByteBuffer, recordBatch, body);
+                return new RecordBatch(Schema, memoryOwner: null, arrays, 
(int)recordBatch.Length);
+            }
+            finally
+            {
+                // Each buffer that views the body holds its own reference to 
it. A compressed body
+                // is viewed only by buffers stored uncompressed, so it is 
often released here.
+                body?.Release();

Review Comment:
   If `BuildArrays` throws after creating a nonempty buffer—for example, 
because a later field or buffer is malformed—the partially built buffers are 
never disposed. This `finally` releases only the reader's reference, leaving 
the body allocated until the abandoned handles' finalizers run, despite the 
documented ownership on failure. Please clean up partially built buffers, 
children and arrays on exceptions, and add a tracking-allocator test that 
verifies immediate release without forcing GC.



-- 
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