yuxihu commented on a change in pull request #14365: Fix memory leak for 
size-zero ndarray
URL: https://github.com/apache/incubator-mxnet/pull/14365#discussion_r265416688
 
 

 ##########
 File path: include/mxnet/ndarray.h
 ##########
 @@ -1055,9 +1054,12 @@ class NDArray {
       }
       size_t aux_bytes = shape.Size() * mshadow::mshadow_sizeof(aux_types[i]);
       if (aux_handles[i].size < aux_bytes) {
-        // free storage if necessary and alloc again
-        if (aux_handles[i].size > 0) Storage::Get()->Free(aux_handles[i]);
-        // init aux storage
+        // set the context for aux handle to make sure we free it to the right 
device.
+        if (aux_handles[i].ctx.dev_type != ctx.dev_type) {
 
 Review comment:
   Yes, test_operator_gpu:test_sparse_nd_elemwise_add failed if context is not 
set here. A new aux handle is created 
[here](https://github.com/apache/incubator-mxnet/blob/master/include/mxnet/ndarray.h#L1054)
 with the default context (CPU) and size(0). Currently we have the size > 0 
check so we do not call Free. After we remove the size > 0 check, it will call 
Free with CPU context, which caused [this 
failure](https://github.com/apache/incubator-mxnet/blob/master/src/storage/storage.cc#L134).
 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to