This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 4/5] Make Python inferior-related internal functions return a gdbpy_inf_ref


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]