From: Phil Muldoon <pmuldoon@redhat.com>
To: Tom Tromey <tromey@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [patch] [python] Implement Inferior.current_inferior
Date: Tue, 28 Jun 2011 22:14:00 -0000 [thread overview]
Message-ID: <m3r56dob7a.fsf@redhat.com> (raw)
In-Reply-To: <m3fwmt66nz.fsf@fleche.redhat.com> (Tom Tromey's message of "Tue, 28 Jun 2011 14:29:52 -0600")
Tom Tromey <tromey@redhat.com> writes:
>>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
>
> Phil> -/* Return a borrowed reference to the Python object of type Inferior
> Phil> +/* Return a reference to the Python object of type Inferior
> Phil> representing INFERIOR. If the object has already been created,
> Phil> - return it, otherwise, create it. Return NULL on failure. */
> Phil> + return it and increment the reference count, otherwise, create it.
> Phil> + Return NULL on failure. */
> Phil> PyObject *
> Phil> inferior_to_inferior_object (struct inferior *inferior)
> Phil> {
> Phil> @@ -154,13 +155,14 @@ inferior_to_inferior_object (struct inferior *inferior)
>
> Phil> do_cleanups (cleanup);
> Phil> }
> Phil> + else
> Phil> + Py_INCREF ((PyObject *)inf_obj);
>
> I don't really follow this.
Well it took me a bit too. inferior_to_inferior_object is used in a
multitude of scenarios. This patchlet addresses a new Python object
created to follow an internal GDB inferior.
Take this example:
foo = gdb.current_inferior()
bar = gdb.current_inferior()
So the first time with foo, a new Python inferior object is created. And
that reference is stashed in the GDB inferior object with
set_inferior_data. The second time, inferior_to_inferior_object queries
the inferior, finds the reference stashed in the GDB inferior and
returns that stashed Python inferior object. But before this patch, bar
would not have had the reference count incremented. This bug was not
really exposed before the current_inferior patch. This patch fixes
that. Why? Because, if you do:
foo = None
Then the reference count = 0, and bar points to garbage after Python
GC's the foo object.
> I think the best model would be that the 'struct inferior' owns a
> reference to the gdb.Inferior object, and then
> inferior_to_inferior_object is consistent: it either always returns a
> new reference or a borrowed reference.
In the context of this patch, I decided against a wholesale rewriting of
how we store inferiors. Now that his patch is split, it might be time
to implement your suggestion.
> For a borrowed reference, I think you can leave this function as-is.
>
> For a new reference, I think you need to remove the 'else' from your
> patch, and update infpy_dealloc to decref.
Functions that return either a borrowed or new references really are
bogus IMO. The consumer does not know what they are getting.
inferior_to_inferior_object does this. This patch uniformly returns a
new reference, and the callers are responsible for the life-cycle of that
reference. While that means a little more busy-work for the callers,
they can be consistent in dealing with the life-cycle of that object.
That being said, I kind of like your idea above.
Cheers,
Phil
prev parent reply other threads:[~2011-06-28 22:14 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-06-27 14:11 Phil Muldoon
2011-06-27 17:35 ` Kevin Pouget
2011-06-27 19:30 ` Phil Muldoon
2011-06-27 20:13 ` Joel Brobecker
2011-06-27 20:27 ` Phil Muldoon
2011-06-28 7:47 ` Kevin Pouget
2011-06-28 8:20 ` Phil Muldoon
2011-06-28 20:31 ` Tom Tromey
2011-06-28 20:30 ` Tom Tromey
2011-06-28 22:14 ` Phil Muldoon [this message]
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=m3r56dob7a.fsf@redhat.com \
--to=pmuldoon@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=tromey@redhat.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