Armin Rigo committed e11a35d

Document some limitations of ffi.gc(), particularly on PyPy.
(transplanted from 4480f85a1279b0f13803821265a096b35b40cefa)

  • Participants
  • Parent commits 00210fd
  • Branches release-0.8

Comments (0)

Files changed (1)

File doc/source/index.rst

 object is garbage-collected.  *New in version 0.3* (together
 with the fact that any cdata object can be weakly referenced).
+Note that this should be avoided for large memory allocations or
+for limited resources.  This is particularly true on PyPy: its GC does
+not know how much memory or how many resources the returned ``ptr``
+holds.  It will only run its GC when enough memory it knows about has
+been allocated (and thus run the destructor possibly later than you
+would expect).  Moreover, the destructor is called in whatever thread
+PyPy is at that moment, which might be a problem for some C libraries.
+In these cases, consider writing a wrapper class with custom ``__enter__()``
+and ``__exit__()`` methods that allocate and free the C data at known
+points in time, and using it in a ``with`` statement.
 .. "versionadded:: 0.3" --- inlined in the previous paragraph
 ``ffi.new_handle(python_object)``: return a non-NULL cdata of type