Re: question: python gc doesn't collect buffer allocated by read_memory()

Tom Tromey
2012-03-26T18:56:30Z

>>>>> ">" == HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com> writes:

>> Sorry for missing. I first found this on gdb-7.2-48.el6.x86_64. I used
>> 7.4 in the presentation of the first mail.

Thanks.

>> On the other hand, it appears to me that buffer objects returned by
>> inferior.read_memory() is never collected by gc.collect().

I think I found the problem.

The issue is that PyBuffer_FromReadWriteObject acquires a reference to
the base object -- but the code in gdb assumed that it stole a reference.

Could you try the appended patch?
It works for me; if it works for you, I will put it in.
If you can't try it, I'll just assume it is ok and go ahead...

Tom

diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 339a221..0f5a6a3 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -405,7 +405,7 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
CORE_ADDR addr, length;
void *buffer = NULL;
membuf_object *membuf_obj;
- PyObject *addr_obj, *length_obj;
+ PyObject *addr_obj, *length_obj, *result;
struct cleanup *cleanups;
volatile struct gdb_exception except;
static char *keywords[] = { "address", "length", NULL };
@@ -457,8 +457,10 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
membuf_obj->addr = addr;
membuf_obj->length = length;

- return PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0,
- Py_END_OF_BUFFER);
+ result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj, 0,
+ Py_END_OF_BUFFER);
+ Py_DECREF (membuf_obj);
+ return result;
}

/* Implementation of gdb.write_memory (address, buffer [, length]).