[pypy-issue] Issue #2149: Memory leak for Python subclass of cpyext object with non-trivial tp_dealloc (pypy/pypy)

Jason Madden issues-reply at bitbucket.org
Fri Oct 2 16:36:48 CEST 2015


New issue 2149: Memory leak for Python subclass of cpyext object with non-trivial tp_dealloc
https://bitbucket.org/pypy/pypy/issues/2149/memory-leak-for-python-subclass-of-cpyext

Jason Madden:

As [discussed on IRC](https://botbot.me/freenode/pypy/2015-09-30/?msg=50868264&page=3) and initially reported to [gevent](https://github.com/gevent/gevent/issues/660), if a pure-Python object subclasses a cpyext object that has a non trivial `tp_dealloc` (e.g., it stores additional instance data in its struct), memory can leak. This is because deallocating the pure-Python instance fails to call the `tp_dealloc` of the superclass.

This is easy to demonstrate with Cython, though Cython is not necessary. Given a `base.pxd` like so:

```python
cdef class Base:
    cdef public object thing
```
and a corresponding `base.py` like so:
```python
class Base(object):
    def __init__(self):
       self.thing = []
```
compile the cython code and then create a subclass in python:
```python
from base import Base
class Sub(Base):
     pass
```

A tool like objgraph will show that allocating instances of `Sub` leaks a new list each time.




More information about the pypy-issue mailing list