在扩展模块中的段错误 [英] segfault in extension module

查看:87
本文介绍了在扩展模块中的段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在C中编写了一个函数来执行蛋白质序列比对。这个

在独立的C程序中运行良好。我添加了必要的包装

在Python中使用它;它返回三个字符串和一个整数。但是,当功能完成后,很快就会出现一个段落错误,而且口译员会因为
而死。


如果我跑Python以交互方式调用该函数会导致

段错误。如果我正在运行一个脚本,我实际上可以打印出返回的

值(这是我所期望的 - 所以有些东西正在运行)但是只要

脚本完成我再次获得段错误。我甚至可以使用不同的参数调用函数

两次 - 它可以同时工作。所以看来

问题在于结束松散的结束。


我如何确定出了什么问题?我在C版本中没有像

这样的问题。我在任何地方都没有使用free() - 我最终将需要修复这个问题,但我找不到任何我可能访问的地方

不可用的内存。 (添加免费()在

模块中没有任何区别,因为它的价值,但我在C程序中遇到了一些问题

所以我把它遗漏了。)


[我在整个C代码中也尝试过使用PyMem_Malloc。

Doesn'' t帮助。]


谢谢,

Nat

(请直接回复!)


I''ve written a function in C to perform protein sequence alignment. This
works fine in a standalone C program. I''ve added the necessary packaging
to use it in Python; it returns three strings and an integer. However, as
soon as the function is complete, I get a segfault and the interpreter
dies.

If I run Python interactively, just calling the function causes a
segfault. If I''m running a script, I can actually print out the return
values (which are what I''d expect - so something''s working) but as soon as
the script is done I get the segfault again. I can even call the function
twice, with different arguments - and it works both times. So it appears
that the problem is with tying up loose ends.

How do I determine what is going wrong? I do not get any problem like
this in the C version. I am not using free() anywhere - I will eventually
need to fix this, but I cannot find any place where I might be accessing
unavailable memory. (Adding in free() does not make any difference in the
module, for what it''s worth, but I''ve had some issues with the C program
so I''ve left it out.)

[I''ve also tried using PyMem_Malloc instead, throughout the C code.
Doesn''t help.]

thanks,
Nat
(please reply directly!)

推荐答案

Nathaniel Echols< ec **** @ OCF.Berkeley.EDU>写道:
Nathaniel Echols <ec****@OCF.Berkeley.EDU> writes:
我如何确定出了什么问题?
How do I determine what is going wrong?




我建议运行你的代码(或交互式python) )在调试器中。

例如,使用gdb,你会得到


gdb / usr / bin / python

(gdb)运行



I recommend to run your code (or the interactive python) in a debugger.
For example, with gdb, you''d get

gdb /usr/bin/python
(gdb) run

import foo
import foo



[程序崩溃]

(gdb)bt


后一个命令会给出一个回溯,它应该告诉你

它崩溃了。如果您没有获得足够的细节,请确保使用调试信息编译

模块。如果你在Windows上,那么make

确保Python也被编译用于调试。


问候,

Martin


[program crashes]
(gdb) bt

The latter command will give a backtrace, which should tell you where
it crashes. If you don''t get enough detail, make sure you compile your
module with debugging information. If you are on Windows, make then
sure that Python is compiled for debugging as well.

Regards,
Martin


Hello Nat,
Hello Nat,
我在C中编写了一个函数来执行蛋白质序列比对。这个
在独立的C程序中运行良好。我添加了必要的包装,以便在Python中使用它;
哪一个?您使用的是C API吗?

它返回三个字符串和一个整数。然而,当函数完成后,我得到一个段错误,解释器就死了。
I''ve written a function in C to perform protein sequence alignment. This
works fine in a standalone C program. I''ve added the necessary packaging
to use it in Python; Which one? Are you using the C API?
it returns three strings and an integer. However, as
soon as the function is complete, I get a segfault and the interpreter
dies.



看起来像引用计数问题,看看
http://www.python.org/doc/current/ ext / refcounts.html


IMO只是避免所有这些东西并使用SWIG / Boost.Python / Pyrex。


HTH。

Miki


Looks like refcount problems, check out
http://www.python.org/doc/current/ext/refcounts.html .

IMO just avoid all this stuff and use SWIG/Boost.Python/Pyrex.

HTH.
Miki


> >我在C中编写了一个函数来执行蛋白质序列比对。这个
> > I''ve written a function in C to perform protein sequence alignment. This
在独立的C程序中运行良好。我添加了必要的包装,以便在Python中使用它;哪一个?您使用的是C API吗?
works fine in a standalone C program. I''ve added the necessary packaging
to use it in Python; Which one? Are you using the C API?




是的 - 我正在阅读手册中的内容。

看起来像引用计数问题,检查
http://www.python.org /doc/current/ext/refcounts.html


之前我读过这篇文章并且无法弄清楚它是什么意思。这看起来似乎与它有关,但我无法弄清楚我做错了什么。

我只有一个叫做纯C的函数函数并返回一个

元组的字符串。我猜我需要在某个地方添加一个Py_INCREF()

,但到目前为止,这只会让它更快地变为段错误。 (我是

不确定用于Py_INCREF()的参数是什么。)


我看了几个其他页面,他们似乎都涉及设置

比我正在做的更复杂。我已经在使用Py_BuildValue()

来生成返回的元组,我的理解是这应该是

避免重大问题。 。 。

IMO只是避免所有这些东西并使用SWIG / Boost.Python / Pyrex。



Yup - I''m reading right out of the manual.
Looks like refcount problems, check out
http://www.python.org/doc/current/ext/refcounts.html .
I read this before and couldn''t figure out what it meant. This does seem
like it would relate, but I can''t figure out what I''m doing incorrectly.
I just have one function which calls a pure C function and returns a
tuple of strings from it. I''m guessing I need to add a Py_INCREF()
somewhere but so far this just makes it segfault sooner. (I''m
not sure what argument to use for Py_INCREF(), either.)

I''ve looked at several other pages, and they all seem to involve setups
more complicated than what I''m doing. I''m already using Py_BuildValue()
to generate the returned tuple, and my understanding is that this should
avoid major problems. . .
IMO just avoid all this stuff and use SWIG/Boost.Python/Pyrex.




我会看看这些,但是我只需要一点点代码我需要做什么

这个 - 我从头开始编写它的意图是使用它这个

方式,并且可以写如果我不关心速度,那就用Python。

我真的会从使用其他方法中受益吗?这里的目标

明确地只将非常依赖时间的代码放在C中;所有的东西

留在Python中。


谢谢,

Nat



I''ll look at these, but I only have a tiny little bit of code I need to do
this with - I coded it from scratch with the intention of using it this
way, and could have written it in Python if I didn''t care about speed.
Would I really benefit from using one of the other methods? The goal here
is explicitly to put only the very time-dependent code in C; everything
else stays in Python.

thanks,
Nat


这篇关于在扩展模块中的段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆