{"action":"closed","issue":{"url":"https://api.github.com/repos/pyopencl/pyopencl/issues/113","repository_url":"https://api.github.com/repos/pyopencl/pyopencl","labels_url":"https://api.github.com/repos/pyopencl/pyopencl/issues/113/labels{/name}","comments_url":"https://api.github.com/repos/pyopencl/pyopencl/issues/113/comments","events_url":"https://api.github.com/repos/pyopencl/pyopencl/issues/113/events","html_url":"https://github.com/pyopencl/pyopencl/issues/113","id":137388646,"number":113,"title":"from_int_ptr and retain/release","user":{"login":"SyamGadde","id":1247460,"avatar_url":"https://avatars.githubusercontent.com/u/1247460?v=3","gravatar_id":"","url":"https://api.github.com/users/SyamGadde","html_url":"https://github.com/SyamGadde","followers_url":"https://api.github.com/users/SyamGadde/followers","following_url":"https://api.github.com/users/SyamGadde/following{/other_user}","gists_url":"https://api.github.com/users/SyamGadde/gists{/gist_id}","starred_url":"https://api.github.com/users/SyamGadde/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SyamGadde/subscriptions","organizations_url":"https://api.github.com/users/SyamGadde/orgs","repos_url":"https://api.github.com/users/SyamGadde/repos","events_url":"https://api.github.com/users/SyamGadde/events{/privacy}","received_events_url":"https://api.github.com/users/SyamGadde/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-02-29T21:54:07Z","updated_at":"2016-03-02T18:08:16Z","closed_at":"2016-03-02T18:08:16Z","body":"For longer than I care to admit, I've been trying to diagnose a persistent issue with memory leaks which I am finally convinced is not ultimately PyOpenCL's fault, but PyOpenCL does not make it easy to avoid it either.\r\n\r\nFor various PyOpenCL classes, PyOpenCL provides a class method `from_int_ptr(clptr)`, to wrap pointers to OpenCL objects that have been created outside of PyOpenCL.  `XXXX.from_int_ptr` automatically calls `clRetainXxxx(clptr)` to increase the reference count before returning the Python wrapper object, and then calls `clReleaseXxxx(clptr)` to decrease the reference count when the Python object is dereferenced.\r\n\r\nAll well and good, _except_ these externally created objects presumably already have a non-zero reference count, and it would be nice to have a way to be able to subsume one of those references within the `from_int_ptr` wrapper or otherwise to be able to decrease the reference count of `clptr` from PyOpenCL.  It doesn't seem there is an easy way to do the latter in PyOpenCL, though one way to do the former is via the following hammer:\r\n\r\n```\r\n--- a/src/c_wrapper/clobj.h\r\n+++ b/src/c_wrapper/clobj.h\r\n@@ -96,7 +96,7 @@ template<typename CLObj>\r\n static PYOPENCL_INLINE CLObj*\r\n clobj_from_int_ptr(intptr_t ptr)\r\n {\r\n-    return new CLObj(reinterpret_cast<typename CLObj::cl_type>(ptr), true);\r\n+    return new CLObj(reinterpret_cast<typename CLObj::cl_type>(ptr), false);\r\n }\r\n \r\n template<typename T, typename T2>\r\n```\r\nThe particular case I've been struggling with uses `gpyfft`, which calls out to `clfft` functions and receives an OpenCL event back.  `gpyfft` then attempts to wrap the events using `Event.from_int_ptr` and returns the wrapped Event.  However, at that point the event has a reference count of 2.  The user (i.e. me) might assume that when the returned Event is dereferenced in Python, the underlying object will eventually disappear, but it remains due to the extra reference, causing a memory leak.\r\n\r\nThe change I included above has been working fine for me, and has eliminated the memory leak (and importantly, not caused any segfaults), but I'm wondering if that's a good general solution.   Is there another way to decrease the reference count (i.e. call clReleaseEvent, clReleaseProgram, etc.) using only PyOpenCL functions?  I'd rather not have to call out to OpenCL directly.  Another option is to pass through (all the way down to clobj_from_int_ptr) a boolean keyword argument \"retain\" which defaults to True to maintain backwards compatibility.\r\n\r\nThanks for your attention!"}}