Are there any Python reference counting/garbage collection gotchas when dealing with C code?
Posted
by Jason Baker
on Stack Overflow
See other posts from Stack Overflow
or by Jason Baker
Published on 2010-05-29T13:13:58Z
Indexed on
2010/05/29
13:22 UTC
Read the original article
Hit count: 284
Just for the sheer heck of it, I've decided to create a Scheme binding to libpython so you can embed Python in Scheme programs. I'm already able to call into Python's C API, but I haven't really thought about memory management.
The way mzscheme's FFI works is that I can call a function, and if that function returns a pointer to a PyObject
, then I can have it automatically increment the reference count. Then, I can register a finalizer that will decrement the reference count when the Scheme object gets garbage collected. I've looked at the documentation for reference counting, and don't see any problems with this at first glance (although it may be sub-optimal in some cases). Are there any gotchas I'm missing?
Also, I'm having trouble making heads or tails of the cyclic garbage collector documentation. What things will I need to bear in mind here? In particular, how do I make Python aware that I have a reference to something so it doesn't collect it while I'm still using it?
© Stack Overflow or respective owner