斯威格C/C++ Python API 连接 - SEGFAULT

发布于 2024-09-01 22:38:15 字数 4430 浏览 2 评论 0原文

我的任务是创建双程序。首先,我启动 C 程序,通过 Python 的 C/C++ API 调用一些 Python 方法。之后调用的方法调用使用 SWIG 创建的函数。在出现分段错误后,我向您展示了我的示例以及 gdb 的回溯。

ma​​in.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()

最后想到的是我的 Makefile & SWIG 配置文件

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

程序被 ./main 调用,有 输出:

    (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 ?? ()

感谢您的帮助和建议,marxin

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文