yuxihu commented on issue #10692: Executor's set_monitor_callback leaks memory 
in high-level languages.
URL: 
https://github.com/apache/incubator-mxnet/issues/10692#issuecomment-487698584
 
 
   @KellenSunderland I changed your script a little bit and I did not see 
memory leak anymore.
   ```
   import ctypes
   import mxnet as mx
   from mxnet.base import NDArrayHandle
   from mxnet.ndarray import NDArray
   
   count = 0
   
   def mon_callback(*args, **kwargs):
       global count
       count = count + 1
       array = ctypes.cast(args[1], NDArrayHandle)
       array = NDArray(array, writable=False)
   ```
   
   I think it is not the best practice to pass a raw pointer back to higher 
level language and rely on users to properly free the memory. I tried to free 
the memory after we execute the callback in 
[GraphExecutor](https://github.com/apache/incubator-mxnet/pull/14805/files). 
However, it caused double free error in some cases where we create NDArray 
using the raw pointer. In this case, the memory is properly freed, as the 
modified code above. This is actually what we are doing in 
[Monitor](https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/monitor.py#L70)
 class.
   
   Please let us know if you have further questions. Otherwise, please close 
the issue. Thanks.

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