Python gc.get_referents() returning references that are unknown to inspect module
Question: I am trying to debug a memory leak and have tracked it down to a single
object, call it "parent".

`gc.get_referents(parent)` indicates that it is effectively gaining more and
more references to the object that is leaking. I'm trying to find out more
information about how it is happening, however, `inspect.getmembers(parent)`
knows nothing about these references that gc.get_referents does know about:

i.e.

    
    
    import gc
    import inspect
    
    parent = someObject()
    dependents = gc.get_referents(parent)
    fromInspect = [b for (a,b) in inspect.getmembers(parent) if b in dependents]
    notFromInspect = [b for (a,b) in inspect.getmembers(parent) if b not in dependents]
    print len(fromInspect)
    >>> 3
    print len(notFromInspect)
    >>> 69
    

So there are lots of objects (69 of them!) that the gc module knows about, but
inspect does not.

How does `gc.get_referents()` construct the list of "referent" objects for a
Python object?

Do I need to look at slots? Something else?


Answer: In my case, it turned out that "parent" was a custom python object implemented
in C/C++.

The documentation here describes how you can write your own "container"
objects in Python:
<https://docs.python.org/2/c-api/typeobj.html#Py_TPFLAGS_HAVE_GC>

All that such objects have to do to then interface with the garbage collector
is provide two function pointers,
[`tp_traverse`](https://docs.python.org/2/c-api/typeobj.html#c.PyTypeObject.tp_traverse)
and `tp_clear`.

`tp_traverse` allows a custom type object to tell the Garbage collector
anything it wants about which objects this object owns.

So in my case, this is how I am seeing references which are unknown to the
`inspect` module.

Unfortunately for me, I still haven't found the actual cause of the leak -
except now I know it's related to the C/C++ code.

