seg. fault with ext. module

Steffen Ries steffen.ries at sympatico.ca
Sat Aug 25 12:32:53 EDT 2001


"Gorny" <gorny at hobbiton.org> writes:

> > You forgot to increment the reference count for None, which creates a
> > problem at termination time.
> >
> > See http://www.python.org/doc/current/ext/refcounts.html
> 
> If I understand it all well, the problem handles about NOT defining
> the reference, so the returned variable stays in memory??

Yes (sort of). The function returns a reference to an existing object
without updating the ref count. The caller (the Interpreter) will
decrement the ref count, when this reference gets out of scope,
which means in this case `None' is disposed too early, because it was
used more often than the ref count said. (Someone with more insight
can explain why this creates a segfault.)

> I have to do this *always* when working with variables in the
> module which can be accessed from python??
> Well thanks in advance...:)

Yes. If you return a reference to an existing object, you have to
increment the ref count. If you create a new object (e.g. with
Py_BuildValue()), you just pass the reference to the caller.

/steffen



More information about the Python-list mailing list