tags:

views:

72

answers:

0

Hello, my task is to create dual program. At the beginning I start C program that calls throught C/C++ API of Python some Python method. The called method after that call a function that is created with SWIG. I show you my sample also with backtrace from gdb after I am given Segmentation fault.

main.c:

#include <Python.h>
#include <stdio.h>

#include "utils.h"

int main(int argc, char** argv)
{
   printf("Calling from C !\n");
   increment();

   int i;
   for(i = 0; i < 11; ++i)
   {

      Py_Initialize();

      PyObject *pname = PyString_FromString("py_function");
      PyObject *module = PyImport_Import(pname);

      PyObject *dict = PyModule_GetDict(module);
      PyObject *func = PyDict_GetItemString(dict, "ink");
      PyObject_CallObject(func, NULL);
      Py_DECREF(module);
      Py_DECREF(pname);

      printf("\tbefore finalize\n");
      Py_Finalize();
      printf("\tafter finalize\n");
   }

   return 0;
}

utils.c

#include <stdio.h>
#include "utils.h"

void increment(void)
{
   printf("Incremention counter to: %u\n", ++counter);
}

py_function.py

#!/usr/bin/python2.6
'''py_function.py - Python source designed to demonstrate the use of python embedding'''

import utils

def ink():
   print 'I am gonna increment !'
   utils.increment()

and last think is my Makefile & SWIG configure file

Makefile:

CC=gcc
CFLAGS=-c -g -Wall -std=c99

all: main

main: main.o utils.o utils_wrap.o
 $(CC) main.o utils.o -lpython2.6 -o sample
 swig -Wall -python -o utils_wrap.c utils.i
 $(CC) utils.o utils_wrap.o -shared -o _utils.so

main.o: main.c
 $(CC) $(CFLAGS) main.c -I/usr/include/python2.6 -o main.o

utils.o: utils.c utils.h
 $(CC) $(CFLAGS) -fPIC utils.c -o $@

utils_wrap.o: utils_wrap.c
 $(CC) -c -fPIC utils_wrap.c -I/usr/include/python2.6 -o $@

clean:

rm -rf *.o

The program is called by ./main and there is output:

    (gdb) run
Starting program: /home/marxin/Programming/python2/sample 
[Thread debugging using libthread_db enabled]
Calling from C !
Incremention counter to: 1
I am gonna increment !
Incremention counter to: 2
 before finalize
 after finalize
I am gonna increment !
Incremention counter to: 3
 before finalize
 after finalize
I am gonna increment !
Incremention counter to: 4
 before finalize
 after finalize

Program received signal SIGSEGV, Segmentation fault.
0xb7ed3e4e in PyObject_Malloc () from /usr/lib/libpython2.6.so.1.0

Backtrace: (gdb) backtrace

#0  0xb7ed3e4e in PyObject_Malloc () from /usr/lib/libpython2.6.so.1.0
#1  0xb7ca2b2c in ?? ()
#2  0xb7f8dd40 in ?? () from /usr/lib/libpython2.6.so.1.0
#3  0xb7eb014c in ?? () from /usr/lib/libpython2.6.so.1.0
#4  0xb7f86ff4 in ?? () from /usr/lib/libpython2.6.so.1.0
#5  0xb7f99820 in ?? () from /usr/lib/libpython2.6.so.1.0
#6  0x00000001 in ?? ()
#7  0xb7f8dd40 in ?? () from /usr/lib/libpython2.6.so.1.0
#8  0xb7f4f014 in _PyObject_GC_Malloc () from /usr/lib/libpython2.6.so.1.0
#9  0xb7f99820 in ?? () from /usr/lib/libpython2.6.so.1.0
#10 0xb7f4f104 in _PyObject_GC_NewVar () from /usr/lib/libpython2.6.so.1.0
#11 0xb7ee8760 in _PyType_Lookup () from /usr/lib/libpython2.6.so.1.0
#12 0xb7f99820 in ?? () from /usr/lib/libpython2.6.so.1.0
#13 0x00000001 in ?? ()
#14 0xb7f8dd40 in ?? () from /usr/lib/libpython2.6.so.1.0
#15 0xb7ef13ed in ?? () from /usr/lib/libpython2.6.so.1.0
#16 0xb7f86ff4 in ?? () from /usr/lib/libpython2.6.so.1.0
#17 0x00000001 in ?? ()
#18 0xbfff0c34 in ?? ()
#19 0xb7e993c3 in ?? () from /usr/lib/libpython2.6.so.1.0
#20 0x00000001 in ?? ()
#21 0xbfff0c70 in ?? ()
#22 0xb7f99da0 in ?? () from /usr/lib/libpython2.6.so.1.0
#23 0xb7f86ff4 in ?? () from /usr/lib/libpython2.6.so.1.0
#24 0xb7f86ff4 in ?? () from /usr/lib/libpython2.6.so.1.0
#25 0x080a6b0c in ?? ()
#26 0x080a6b0c in ?? ()
#27 0xb7e99420 in PyObject_CallFunctionObjArgs () from /usr/lib/libpython2.6.so.1.0
#28 0xb7f86ff4 in ?? () from /usr/lib/libpython2.6.so.1.0
#29 0xb7f86ff4 in ?? () from /usr/lib/libpython2.6.so.1.0
#30 0x800e55eb in ?? ()
#31 0x080a6b0c in ?? ()
#32 0xb7e9958c in PyObject_IsSubclass () from /usr/lib/libpython2.6.so.1.0
#33 0xb7f8dd40 in ?? () from /usr/lib/libpython2.6.so.1.0
#34 0x080a9020 in ?? ()
#35 0xb7fb78f0 in PyFPE_counter () from /usr/lib/libpython2.6.so.1.0
#36 0xb7f86ff4 in ?? () from /usr/lib/libpython2.6.so.1.0
#37 0x00000000 in ?? ()

Thanks for your help and advices, marxin