From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 67582 invoked by alias); 24 Jan 2017 16:15:26 -0000 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 Received: (qmail 67152 invoked by uid 89); 24 Jan 2017 16:15:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_SOFTFAIL autolearn=no version=3.3.2 spammy=H*u:1.2.3, H*UA:1.2.3 X-HELO: simark.ca Received: from simark.ca (HELO simark.ca) (158.69.221.121) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 24 Jan 2017 16:15:21 +0000 Received: by simark.ca (Postfix, from userid 33) id F381B1E166; Tue, 24 Jan 2017 11:15:19 -0500 (EST) To: Simon Marchi Subject: Re: [PATCH 4/5] Make Python inferior-related internal functions return a gdbpy_inf_ref X-PHP-Originating-Script: 33:rcube.php MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Tue, 24 Jan 2017 16:15:00 -0000 From: Simon Marchi Cc: gdb-patches@sourceware.org In-Reply-To: <20170123224004.8893-5-simon.marchi@ericsson.com> References: <20170123224004.8893-1-simon.marchi@ericsson.com> <20170123224004.8893-5-simon.marchi@ericsson.com> Message-ID: X-Sender: simon.marchi@polymtl.ca User-Agent: Roundcube Webmail/1.2.3 X-IsSubscribed: yes X-SW-Source: 2017-01/txt/msg00498.txt.bz2 On 2017-01-23 17:40, Simon Marchi wrote: > * I used .release() on the reference in create_thread_object with a > comment explaining why, but I would need some more pairs of eyes on > that to see if it's right. Ok, so I've looked at this a bit more, and I think that my comment is wrong, but the code is right (meaning that the comment didn't match the code in the first place). > diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c > index c7553310c3..79fb5d12d1 100644 > --- a/gdb/python/py-infthread.c > +++ b/gdb/python/py-infthread.c > @@ -46,7 +46,11 @@ create_thread_object (struct thread_info *tp) > return NULL; > > thread_obj->thread = tp; > - thread_obj->inf_obj = find_inferior_object (ptid_get_pid > (tp->ptid)); > + > + /* The thread holds a weak reference to its inferior to avoid > creating a > + reference loop between the inferior and its threads. */ > + gdbpy_inf_ref inf_obj_ref = find_inferior_object (ptid_get_pid > (tp->ptid)); > + thread_obj->inf_obj = inf_obj_ref.release (); The Thread objects do hold strong references to their Inferior object, and it's not really a problem. The only way for an Inferior object to be deallocated is if the corresponding gdb inferior object is removed. A prerequisite of this happening is that all its threads have exited (either by finishing themselves or by being killed by gdb). When a thread exits, we remove the reference from the Inferior to the Thread. If no other reference to the Thread exist (e.g. a Python variable), the Thread will be deallocated immediately, removing the reference it had to the Inferior. The reference cycle should only exist while the threads are running, which is not a state in which we can nor want to deallocate the Inferior object. As soon as the threads exit, the cycle is broken. So the .release() is right, the corresponding DECREF is in thpy_dealloc.