From patchwork Tue Nov 12 19:35:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 35840 Received: (qmail 66173 invoked by alias); 12 Nov 2019 19:35:11 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 66165 invoked by uid 89); 12 Nov 2019 19:35:11 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy=H*F:D*be, 1115, H*Ad:D*be, HContent-Transfer-Encoding:8bit X-HELO: mailsec106.isp.belgacom.be Received: from mailsec106.isp.belgacom.be (HELO mailsec106.isp.belgacom.be) (195.238.20.102) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 12 Nov 2019 19:35:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1573587309; x=1605123309; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=OQFoqsrA8xejPVib9+DD5lSyKgPQ2ntK4l0JYZd0HaU=; b=eDKMeXHPC1LlsG07yrzPQQzqNgs1r1aQ98iRhkK28HSczEq8g0V+dqGq oRFxQ0v2k4fjLaTHQtTv+MrNHQDf3w==; IronPort-SDR: vQzDkdi2C+I7j7hVtYePjv79uHGawr8NKU/sNoVgVIFoeBc5DnJRpSAvx6lSsKCNRSam+ZtMCR Eb3RBx/KsheDYtDFavWlVS8e+mVwCn8BjgygIWIfQoSUU2rByCQvu+dwI24rZs1yCd3SftN5UM LiIIypjR3CwwFSDhBKRqQ3FeP8pTDy01PAeZ9qtN/KuFAK05BukDgPpP4c3nWAZaKKXeKaFBgh x7qiqRJQ+hVfEOkSK0Crxbt8WE2Ba7IvZquW3JT7I7J2lGcIS+Jstf6D3G1bMiY9OmS1QXKc9I HvI= Received: from 117.79-136-217.adsl-dyn.isp.belgacom.be (HELO md.home) ([217.136.79.117]) by relay.skynet.be with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 12 Nov 2019 20:35:06 +0100 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFA] Fix python gdbpy_breakpoint_object leak. Date: Tue, 12 Nov 2019 20:35:01 +0100 Message-Id: <20191112193501.21170-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes valgrind reports a leak when a breakpoint is created then deleted: ==1313== 40 bytes in 1 blocks are definitely lost in loss record 1,115 of 8,596 ==1313== at 0x4835753: malloc (vg_replace_malloc.c:307) ==1313== by 0x6E05BC: _PyObject_New (object.c:255) ==1313== by 0x470E4B: gdbpy_breakpoint_created(breakpoint*) (py-breakpoint.c:1023) ==1313== by 0x2946D9: operator() (std_function.h:687) ==1313== by 0x2946D9: notify (observable.h:106) ==1313== by 0x2946D9: install_breakpoint(int, std::unique_ptr >&&, int) (breakpoint.c:8136) ==1313== by 0x295BCA: create_breakpoint_sal (breakpoint.c:8878) ==1313== by 0x295BCA: create_breakpoints_sal (breakpoint.c:8919) ==1313== by 0x295BCA: create_breakpoints_sal_default (breakpoint.c:13671) ... The leak is due to a superfluous Py_INCREF when the python object is allocated inside gdbpy_breakpoint_created, when the python object is allocated locally: this object has already a refcount of 1, and the only reference is the reference from the C breakpoint object. The Py_INCREF is however needed when the python object was created from python: the python object was stored in bppy_pending_object, and gdbpy_breakpoint_created creates a new reference to this object. Solve the leak by calling 'Py_INCREF (newbp);' only in the bppy_pending_object case. Regression tested on debian/amd64 natively and under valgrind on centos/amd64. Before the patch, 795 tests have a definite leak. After the patch, 197 have a definite leak. Thanks to Tom, that helped on irc with the python refcount logic. gdb/ChangeLog YYYY-MM-DD Philippe Waroquiers * python/py-finishbreakpoint.c (gdbpy_breakpoint_created): only call Py_INCREF (newbp) in the bppy_pending_object case. --- gdb/python/py-breakpoint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index 65cb29fdca..4170737416 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -1017,6 +1017,7 @@ gdbpy_breakpoint_created (struct breakpoint *bp) if (bppy_pending_object) { newbp = bppy_pending_object; + Py_INCREF (newbp); bppy_pending_object = NULL; } else @@ -1027,7 +1028,6 @@ gdbpy_breakpoint_created (struct breakpoint *bp) newbp->bp = bp; newbp->bp->py_bp_object = newbp; newbp->is_finish_bp = 0; - Py_INCREF (newbp); ++bppy_live; } else