core dumps in printobject()

Andy Bensky (ab@infoseek.com)
Thu, 10 Mar 1994 11:42:03 +0800

I have an application that links in at run time a shared object
library. Then I pass into the C module a pointer to a class object and
an instance of a differnet class. The C module uses newlistobject(0)
to create an empty list, then it creates a number of instances of the
class whos handle was passed in previously using
call_object(class_handle, arglist for __init__ func created with
mkvalue). Each time it creates a new instance it adds it to the list
using addlistitem(). Once the list is complete it is made an attribute of
the instance that was passed in using a method defined in python to set
the attribute. The method is accessed using
call_object(getattr(instance,'methodname'),value created using mkvalue)

When running the module function that builds the list and adds it to the
instance I generally check __dict__ of the instance after the C function
retruns to see if all went well. What I saw is about half the time examining
either __dict__ or the list itself after the C function returns causes
Python to hang after printing most of the dictionary up to the list. Hitting
ctrl-C causes a core dump due to either a bus error or seg fault. It varies.
The core dump is always somewhere in or around the printobject() function.

Is it possible that printobject() is getting lost if some of the objects
have been garbage collected? My guess is that I'm missing some of the
places it is necessary to do an INCREF on an object created in a C function.
Assuming that is the case then I have some questions about when to INCREF.

1. Is it necessary to INCREF a list object created using newlistobject(0)?
2. Is it necessary to INCREF a new class instance created by using call_object
on the class object itself? (or does call_object INCREF the new instance
before returning?)
3. Since the list is added to the instance by calling an instance method
directly from C I assume the instance method, which is actually compiled
python code, takes care of incrementing the reference count on the list
and it is therefore not necessary to INCREF the list when adding it as an
attribute to another instance in this way.

I added an INCREF to the list just after it was created and to each instance
as created before being added to the list and I can't make python hang or
core dump anymore, so I believe that this is what was causing the intermittent
problem. Although since the problem was intermittent I am not comfortable
assuming that was the solution simply because I can't make it happen again
by trying over and over. If I could get an answer to the questions above
that verifies my assumptions I'd be much happier. Any other details on when
it is necessary to INCREF an object from C will also be helpful and appreciated.

Thanks,

Andy Bensky
InfoSeek Corp.