From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23849 invoked by alias); 28 Mar 2012 17:37:58 -0000 Received: (qmail 23761 invoked by uid 22791); 28 Mar 2012 17:37:57 -0000 X-SWARE-Spam-Status: No, hits=-6.9 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 28 Mar 2012 17:37:44 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q2SHbgSD011506 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 28 Mar 2012 13:37:42 -0400 Received: from barimba (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q2SHbeuI007513 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Wed, 28 Mar 2012 13:37:41 -0400 From: Tom Tromey To: HATAYAMA Daisuke Cc: gdb-patches@sourceware.org Subject: Re: question: python gc doesn't collect buffer allocated by read_memory() References: <87iphzhtvd.fsf@fleche.redhat.com> <20120321.131547.347143263.d.hatayama@jp.fujitsu.com> <87iphrs3ip.fsf@fleche.redhat.com> <20120327.105206.71087856.d.hatayama@jp.fujitsu.com> Date: Wed, 28 Mar 2012 17:37:00 -0000 In-Reply-To: <20120327.105206.71087856.d.hatayama@jp.fujitsu.com> (HATAYAMA Daisuke's message of "Tue, 27 Mar 2012 10:52:06 +0900 ( )") Message-ID: <871uoc1va3.fsf@fleche.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.94 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-03/txt/msg00955.txt.bz2 >>>>> ">" == HATAYAMA Daisuke 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 * python/py-inferior.c (infpy_read_memory): Remove cleanups and explicitly free 'buffer' on exit paths. Decref 'membuf_object' before returning. diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 339a221..06d3272 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -405,8 +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; - struct cleanup *cleanups; + PyObject *addr_obj, *length_obj, *result; volatile struct gdb_exception except; static char *keywords[] = { "address", "length", NULL }; @@ -414,8 +413,6 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) &addr_obj, &length_obj)) return NULL; - cleanups = make_cleanup (null_cleanup, NULL); - TRY_CATCH (except, RETURN_MASK_ALL) { if (!get_addr_from_python (addr_obj, &addr) @@ -426,39 +423,38 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) } buffer = xmalloc (length); - make_cleanup (xfree, buffer); read_memory (addr, buffer, length); } if (except.reason < 0) { - do_cleanups (cleanups); + xfree (buffer); GDB_PY_HANDLE_EXCEPTION (except); } if (error) { - do_cleanups (cleanups); + xfree (buffer); return NULL; } membuf_obj = PyObject_New (membuf_object, &membuf_object_type); if (membuf_obj == NULL) { + xfree (buffer); PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory buffer object.")); - do_cleanups (cleanups); return NULL; } - discard_cleanups (cleanups); - membuf_obj->buffer = buffer; 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]).