![](https://secure.gravatar.com/avatar/99ae038a1c3ad5ba5f674c07b42e5558.jpg?s=120&d=mm&r=g)
Hi I'm having problems with garbage collection I wrote an extension which creates an array and returns it foo() { arr = (PyArrayObject *) PyArray_FromDims( ..... ret = Py_BuildValue("O", arr); return ret; } but now if I do while 1: a=foo() memory is never free'd. I've even tried explicitly calling gc.collect and adding del(a) after a=foo. Is the problem that Py_BuildValue increases the reference count? Mathew
![](https://secure.gravatar.com/avatar/91ef31b78d37324fb6b86073e869537c.jpg?s=120&d=mm&r=g)
I believe PyArray_FromDims() returns a new reference to the object (arr) and that the Py_BuildValue creates another reference so that you've got two references to that array and python is only going to DECREF one when it's done. I would suggest either 1) using the 'N' format character to Py_BuildValue so that another reference isn't created or 2) explicitly calling Py_DECREF(arr) just before you return. Reggie
>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<
On 2/22/02, 4:19:15 PM, Mathew Yeates <mathew@fugue.jpl.nasa.gov> wrote regarding [Numpy-discussion] memory not being freed:
memory is never free'd. I've even tried explicitly calling gc.collect and adding del(a) after a=foo.
Is the problem that Py_BuildValue increases the reference count?
Mathew
![](https://secure.gravatar.com/avatar/56475d2e8acb48b4308f609982f94440.jpg?s=120&d=mm&r=g)
You just want ret = (PyObject*) arr; I assume it is PyObject *foo() and you just didn't show it. -----Original Message----- From: numpy-discussion-admin@lists.sourceforge.net [mailto:numpy-discussion-admin@lists.sourceforge.net] On Behalf Of Mathew Yeates Sent: Friday, February 22, 2002 4:19 PM To: numpy-discussion@lists.sourceforge.net Subject: [Numpy-discussion] memory not being freed Hi I'm having problems with garbage collection I wrote an extension which creates an array and returns it foo() { arr = (PyArrayObject *) PyArray_FromDims( ..... ret = Py_BuildValue("O", arr); return ret; } but now if I do while 1: a=foo() memory is never free'd. I've even tried explicitly calling gc.collect and adding del(a) after a=foo. Is the problem that Py_BuildValue increases the reference count? Mathew _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/4d021a1d1319f36ad861ebef0eb5ba44.jpg?s=120&d=mm&r=g)
I'm proposing the addition of the following semantics to Numeric Arrays in order to support indexing arrays using mask and index arrays. Notice that the change required to support this behavior is minimal and all the work can be backloaded to the user. Let the array_subscript C code check to see if the index object passed in is a Tuple with the last element being a callable method. If this is the case, then the C code calls the callable method to handle the array assignment or array indexing code. So, for example one might say. a[a<3,mask] = 10 where mask is a callable method (or Cobject) which does effectively but has the right calling convention. putmask(a, a<3, 10). This same semantics could also handle integer indexing of arrays (using multiple styles). All that's required is a simple test in Numeric that does not break any current code. What do you think? Could I add the necessary check to support this in Numeric? Do others have a better idea? I know numarray is the solution to all of our problems. But, something tells me that the current version of Numeric is going to be around for a little while and it would be nice for it to have some of these useful features. -Travis
![](https://secure.gravatar.com/avatar/91ef31b78d37324fb6b86073e869537c.jpg?s=120&d=mm&r=g)
I believe PyArray_FromDims() returns a new reference to the object (arr) and that the Py_BuildValue creates another reference so that you've got two references to that array and python is only going to DECREF one when it's done. I would suggest either 1) using the 'N' format character to Py_BuildValue so that another reference isn't created or 2) explicitly calling Py_DECREF(arr) just before you return. Reggie
>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<
On 2/22/02, 4:19:15 PM, Mathew Yeates <mathew@fugue.jpl.nasa.gov> wrote regarding [Numpy-discussion] memory not being freed:
memory is never free'd. I've even tried explicitly calling gc.collect and adding del(a) after a=foo.
Is the problem that Py_BuildValue increases the reference count?
Mathew
![](https://secure.gravatar.com/avatar/56475d2e8acb48b4308f609982f94440.jpg?s=120&d=mm&r=g)
You just want ret = (PyObject*) arr; I assume it is PyObject *foo() and you just didn't show it. -----Original Message----- From: numpy-discussion-admin@lists.sourceforge.net [mailto:numpy-discussion-admin@lists.sourceforge.net] On Behalf Of Mathew Yeates Sent: Friday, February 22, 2002 4:19 PM To: numpy-discussion@lists.sourceforge.net Subject: [Numpy-discussion] memory not being freed Hi I'm having problems with garbage collection I wrote an extension which creates an array and returns it foo() { arr = (PyArrayObject *) PyArray_FromDims( ..... ret = Py_BuildValue("O", arr); return ret; } but now if I do while 1: a=foo() memory is never free'd. I've even tried explicitly calling gc.collect and adding del(a) after a=foo. Is the problem that Py_BuildValue increases the reference count? Mathew _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/4d021a1d1319f36ad861ebef0eb5ba44.jpg?s=120&d=mm&r=g)
I'm proposing the addition of the following semantics to Numeric Arrays in order to support indexing arrays using mask and index arrays. Notice that the change required to support this behavior is minimal and all the work can be backloaded to the user. Let the array_subscript C code check to see if the index object passed in is a Tuple with the last element being a callable method. If this is the case, then the C code calls the callable method to handle the array assignment or array indexing code. So, for example one might say. a[a<3,mask] = 10 where mask is a callable method (or Cobject) which does effectively but has the right calling convention. putmask(a, a<3, 10). This same semantics could also handle integer indexing of arrays (using multiple styles). All that's required is a simple test in Numeric that does not break any current code. What do you think? Could I add the necessary check to support this in Numeric? Do others have a better idea? I know numarray is the solution to all of our problems. But, something tells me that the current version of Numeric is going to be around for a little while and it would be nice for it to have some of these useful features. -Travis
participants (4)
-
Mathew Yeates
-
Paul F Dubois
-
Reggie Dugard
-
Travis Oliphant