public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Simon Marchi <simon.marchi@ericsson.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 4/5] Make Python inferior-related internal functions  return a gdbpy_inf_ref
Date: Tue, 24 Jan 2017 16:15:00 -0000	[thread overview]
Message-ID: <cf8a3293dc83a12f88534a94856556fb@polymtl.ca> (raw)
In-Reply-To: <20170123224004.8893-5-simon.marchi@ericsson.com>

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.

  reply	other threads:[~2017-01-24 16:15 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-23 22:43 [PATCH 0/5] Improve Python Inferior reference handling + fix a bug Simon Marchi
2017-01-23 22:43 ` [PATCH 1/5] Introduce specialized versions of gdbpy_ref Simon Marchi
2017-01-24 15:54   ` Tom Tromey
2017-01-24 16:18     ` Simon Marchi
2017-02-09 11:58   ` Pedro Alves
2017-02-09 16:18     ` Simon Marchi
2017-01-23 22:43 ` [PATCH 3/5] Make Python inferior-related internal functions return inferior_object* Simon Marchi
2017-01-24  0:03   ` Pedro Alves
2017-01-23 22:43 ` [PATCH 5/5] Add missing incref when creating Inferior Python object Simon Marchi
2017-02-25 18:41   ` Simon Marchi
2017-04-27 21:13     ` [pushed master+8.0] " Simon Marchi
2017-01-23 22:43 ` [PATCH 2/5] Add Python Inferior object debug traces Simon Marchi
2017-01-23 22:43 ` [PATCH 4/5] Make Python inferior-related internal functions return a gdbpy_inf_ref Simon Marchi
2017-01-24 16:15   ` Simon Marchi [this message]
2017-02-09 12:30   ` Pedro Alves
2017-02-09 16:39     ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cf8a3293dc83a12f88534a94856556fb@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@ericsson.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).