gemini-code-assist[bot] commented on code in PR #260:
URL: https://github.com/apache/tvm-ffi/pull/260#discussion_r2518832392


##########
python/tvm_ffi/cython/tensor.pxi:
##########
@@ -100,6 +86,23 @@ cdef inline int _from_dlpack_versioned(
     raise ValueError("Expect a dltensor_versioned field, PyCapsule can only be 
consumed once")
 
 
+cdef inline int _from_dlpack_exchange_api(
+    object ext_tensor, DLPackExchangeAPI* exchange_api, int require_alignment,
+    int require_contiguous, TVMFFIObjectHandle* out
+) except -1:
+    cdef DLManagedTensorVersioned* temp_managed_tensor
+    cdef PyObject* ext_tensor_pyobj = <PyObject*>ext_tensor
+    if exchange_api.managed_tensor_from_py_object_no_sync(ext_tensor_pyobj, 
&temp_managed_tensor) != 0:
+        return -1
+
+    # Convert to TVM Tensor
+    if TVMFFITensorFromDLPackVersioned(
+        temp_managed_tensor, require_alignment, require_contiguous, out
+    ) != 0:
+        raise BufferError("Failed to convert DLManagedTensorVersioned to 
ffi.Tensor")

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   There is a potential memory leak in this error handling path. If 
`TVMFFITensorFromDLPackVersioned` fails, it returns a non-zero value, and a 
`BufferError` is raised. However, the `temp_managed_tensor` obtained from 
`managed_tensor_from_py_object_no_sync` is not cleaned up. The 
`DLManagedTensorVersioned` struct has a `deleter` function pointer that must be 
called to release its resources. Since `TVMFFITensorFromDLPackVersioned` is 
expected to take ownership of (i.e., consume) the tensor, its failure implies 
it did not consume it, leaving the current function responsible for cleanup. 
Please ensure the deleter is called on failure to prevent memory leaks.
   
   ```
       if TVMFFITensorFromDLPackVersioned(
           temp_managed_tensor, require_alignment, require_contiguous, out
       ) != 0:
           if temp_managed_tensor.deleter != NULL:
               temp_managed_tensor.deleter(temp_managed_tensor)
           raise BufferError("Failed to convert DLManagedTensorVersioned to 
ffi.Tensor")
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to