C Extension Seg Faults

D

Daryl

I'm having trouble tracking down some segmentation faults with a C
extension. I can see where it's failing but don't know how to fix
it. Basically, my python programme builds a pair of large tuples of
tuples. I want to unpack these tuples and process them. The code in
question looks something like:

.... snip ...

SizeN = PyTuple_Size(PlacementN);
SizeP = PyTuple_Size(PlacementP);
for(ixN = 0; ixN < SizeN; ixN++) {
LenN = (int) PyTuple_Size(PyList_GetItem(PlacementN, ixN));
for(Count = 0; Count < LenN; Count++)
*(GatesN+Count) = (int)
PyInt_AsLong(PyTuple_GetItem(PyTuple_GetItem(PyList_GetItem(PlacementN,
ixN), Count), GATE));
for(ixP = 0; ixP < SizeP; ixP++) {
printf("I feel lucky (%d)...", ixP); fflush(stdout);
LenP = (int) PyTuple_Size(PyList_GetItem(PlacementP,
ixP));
printf("Ta-Da!\n"); fflush(stdout);
for(Count = 0; Count < LenP; Count++)
*(GatesP+Count) = (int)
PyInt_AsLong(PyTuple_GetItem(PyTuple_GetItem(PyList_GetItem(PlacementP,
ixP), Count), GATE));
[ MUCH PROCESSING ]
.... snip ...

The inner loop runs a few hundred times (263) then fails with a
segmentation fault. I've trolled through the documentation online and
in the various .h files but can't seem to find anything.

Anybody have an idea what really obvious foolish thing I'm doing?

Thanks,
-Daryl
 
P

Paul Rubin

Daryl said:
The inner loop runs a few hundred times (263) then fails with a
segmentation fault. I've trolled through the documentation online and
in the various .h files but can't seem to find anything.

Anybody have an idea what really obvious foolish thing I'm doing?

It's not worth staring at code to try to figure out things like that.
Run it under gdb, examine the relevant data objects when it crashes
and see what's broken (e.g. some pointer is unexpectedly null), then
set a conditional breakpoint that stops execution when that incorrect
condition happens (e.g. the pointer gets set to null), run the program
til the breakpoint triggers, see what other condition X caused the
condition that caused the crash, set another breakpoint that triggers
when X occurs, etc. Work backwards systematically til you find the bug.
 
D

Daryl

It's not worth staring at code to try to figure out things like that.
Run it under gdb, examine the relevant data objects when it crashes
and see what's broken (e.g. some pointer is unexpectedly null), then
set a conditional breakpoint that stops execution when that incorrect
condition happens (e.g. the pointer gets set to null), run the program
til the breakpoint triggers, see what other condition X caused the
condition that caused the crash, set another breakpoint that triggers
when X occurs, etc. Work backwards systematically til you find the bug.

Yeah, not 30 seconds after I posted it I found the problem. Under
some (funny) conditions the extension would be called with a list
instead of a tuple. My debug case (of course) worked fine but the
full test would fail.

Sorry for the noise,
-Daryl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,592
Members
45,103
Latest member
VinaykumarnNevatia
Top