[RFA] fix ref counting of inferior_to_inferior_object

Doug Evans
2013-09-12T22:27:42Z

Hi.
While adding an inferior attribute to the newobjfile event,
I noticed inferior_to_inferior_object sometimes returns (AFAICT)
a new reference and sometimes returns a borrowed reference.

This patch makes it consistently return a borrowed reference
(for consistency with obfile_to_objfile_object), and then
updates the rest of the code.

Regression tested on amd64-linux.
Ok to check in?

2013-09-12 Doug Evans <dje@google.com>

* python/py-exitedevent.c (create_exited_event_object): Add comment.
* python/py-inferior.c (inferior_to_inferior_object): Always return
a borrowed reference.
(find_inferior_object): Return a new reference to the inferior object.
(build_inferior_list): Update, inferior_to_inferior_object returns
a borrowed reference.
(gdbpy_selected_inferior): Ditto.

Index: python/py-exitedevent.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-exitedevent.c,v
retrieving revision 1.8
diff -u -p -r1.8 py-exitedevent.c
--- python/py-exitedevent.c 20 May 2013 20:09:01 -0000 1.8
+++ python/py-exitedevent.c 12 Sep 2013 22:19:49 -0000
@@ -49,6 +49,8 @@ create_exited_event_object (const LONGES
goto fail;
}

+ /* Note that inferior_to_inferior_object returns a borrowed reference,
+ so we don't need a decref here. */
inf_obj = inferior_to_inferior_object (inf);
if (!inf_obj || evpy_add_attribute (exited_event,
"inferior",
Index: python/py-inferior.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-inferior.c,v
retrieving revision 1.36
diff -u -p -r1.36 py-inferior.c
--- python/py-inferior.c 18 Jun 2013 18:43:27 -0000 1.36
+++ python/py-inferior.c 12 Sep 2013 22:19:50 -0000
@@ -161,8 +161,9 @@ python_new_objfile (struct objfile *objf

/* Return a reference to the Python object of type Inferior
representing INFERIOR. If the object has already been created,
- return it and increment the reference count, otherwise, create it.
+ return it, otherwise create it. The result is a borrowed reference.
Return NULL on failure. */
+
PyObject *
inferior_to_inferior_object (struct inferior *inferior)
{
@@ -180,15 +181,12 @@ inferior_to_inferior_object (struct infe
inf_obj->nthreads = 0;

set_inferior_data (inferior, infpy_inf_data_key, inf_obj);
-
}
- else
- Py_INCREF ((PyObject *)inf_obj);

return (PyObject *) inf_obj;
}

-/* Finds the Python Inferior object for the given PID. Returns a
+/* Finds the Python Inferior object for the given PID. Returns a new
reference, or NULL if PID does not match any inferior object. */

PyObject *
@@ -197,7 +195,12 @@ find_inferior_object (int pid)
struct inferior *inf = find_inferior_pid (pid);

if (inf)
- return inferior_to_inferior_object (inf);
+ {
+ PyObject *inf_obj = inferior_to_inferior_object (inf);
+
+ Py_XINCREF (inf_obj);
+ return inf_obj;
+ }

return NULL;
}
@@ -384,7 +387,6 @@ build_inferior_list (struct inferior *in
return 0;

success = PyList_Append (list, inferior);
- Py_DECREF (inferior);

if (success)
return 1;
@@ -768,12 +770,16 @@ py_free_inferior (struct inferior *inf,
}

/* Implementation of gdb.selected_inferior() -> gdb.Inferior.
- Returns the current inferior object. */
+ Returns a new reference to the current inferior object. */

PyObject *
gdbpy_selected_inferior (PyObject *self, PyObject *args)
{
- return inferior_to_inferior_object (current_inferior ());
+ PyObject *inf_obj;
+
+ inf_obj = inferior_to_inferior_object (current_inferior ());
+ Py_XINCREF (inf_obj);
+ return inf_obj;
}

int