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

HATAYAMA Daisuke
2012-03-29T00:49:25Z

From: Tom Tromey <tromey@redhat.com>
Subject: Re: question: python gc doesn't collect buffer allocated by read_memory()
Date: Wed, 28 Mar 2012 11:37:40 -0600

>>>>>> ">" == HATAYAMA Daisuke <d.hatayama@jp.fujitsu.com> writes:
>
>>> I suspect another objects allocated remain while not collected.
>
> Thanks, you are correct.
>
> I used valgrind --tool=massif to find the problem.
>
> TRY_CATCH clears the cleanup chain, so making a cleanup in a TRY_CATCH
> and then trying to run or discard it outside the TRY_CATCH will not
> work; instead it leaks the cleanup.
>
> I am checking in the appended patch. It fixes both leaks. Verified
> with massif.
>
> Tom
>
> 2012-03-28 Tom Tromey <tromey@redhat.com>
>
> * python/py-inferior.c (infpy_read_memory): Remove cleanups and
> explicitly free 'buffer' on exit paths. Decref 'membuf_object'
> before returning.
>

After applying this patch, I no longer see any memory leak.

Again, thanks for your help, Tom.

(gdb) shell cat ./testpro.py
import gdb
import gc

i = gdb.inferiors()[0]
buf = gdb.parse_and_eval('buf')

count = 100000
while count >= 0:
i.read_memory(buf.address, buf.type.sizeof)
count -= 1
gc.collect()
(gdb) shell ps aux | head -n 1
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
(gdb) shell ps aux | grep gdb | grep -v grep
hat 28071 0.6 0.3 81964 12364 pts/0 S+ 09:41 0:00 /media/pub/repos/gdb/gdb/gdb ./testpro ./core.27403
(gdb) source ./testpro.py
(gdb) shell ps aux | grep gdb | grep -v grep
hat 28071 2.3 0.3 81968 12500 pts/0 S+ 09:41 0:00 /media/pub/repos/gdb/gdb/gdb ./testpro ./core.27403
(gdb) source ./testpro.py
(gdb) shell ps aux | grep gdb | grep -v grep
hat 28071 2.7 0.3 81968 12500 pts/0 S+ 09:41 0:01 /media/pub/repos/gdb/gdb/gdb ./testpro ./core.27403
(gdb) source ./testpro.py
sh(gdb) shell ps aux | grep gdb | grep -v grep
hat 28071 2.9 0.3 81968 12500 pts/0 S+ 09:41 0:02 /media/pub/repos/gdb/gdb/gdb ./testpro ./core.27403