python - 使用 ctypes 和线程时出现段错误

标签 python c multithreading segmentation-fault ctypes

我使用 ctypes 为 C 库编写了一个 Python 包装器。一切正常,直到我尝试使用 threading 模块。

这是一个重现它的小例子:

foo.c

#include <stdlib.h>
#include <assert.h>

char *foo_init() {
    char *result = (char*)malloc(sizeof(char));
    assert(result);
    return result;
}

char foo_get(const char* p) {
    return *p;
}

void foo_set(char *p, char value) {
    *p = value;
}

void foo_del(char *p) {
    free(p);
}

foo.py

import threading
import ctypes
libfoo=ctypes.CDLL('foo.so')

libfoo.foo_init.restype = ctypes.c_void_p
libfoo.foo_get.restype = ctypes.c_char

class Foo:
    def __init__(self):
        self.__obj__ = libfoo.foo_init()

    def get(self):
        return libfoo.foo_get(self.__obj__)

    def set(self, value):
        libfoo.foo_set(self.__obj__, ctypes.c_char(value))

    def __del__(self):
        libfoo.foo_del(self.__obj__)

x = Foo()
x.set(b'x')
print(x.get())

def compute():
    y = Foo()
    y.set(b'y')
    print(y.get())

t = threading.Thread(target=compute)
t.start()
t.join()

编译:

gcc -Wall -shared -o foo.so -fPIC foo.c

执行:

export LD_LIBRARY_PATH=.
python3 foo.py

结果:

b'x'
[1]    8627 segmentation fault  python3 foo.py

我们在这里看到 x.get() 被正确打印,而 y.get() 出现了段错误。因此,似乎存在与 ctypesthreading 相关的问题。

请注意,如果我将 y 的初始化移到 compute 函数之外,那么程序会正常退出。

此外,每个操作指针的函数都会产生段错误(例如,如果我只保留 __init____del__ 函数,仍然会发生崩溃)。

知道如何解决这个问题吗?

最佳答案

@user2357112 在评论中给了我答案:添加(好的)返回类型和参数类型很重要。

foo.py 的开头替换为以下内容可解决问题:

libfoo.foo_init.restype = ctypes.POINTER(ctypes.c_char)
libfoo.foo_init.argtypes = []
libfoo.foo_get.restype = ctypes.c_char
libfoo.foo_get.argtypes = [ctypes.POINTER(ctypes.c_char)]
libfoo.foo_set.argtypes = [ctypes.POINTER(ctypes.c_char), ctypes.c_char]
libfoo.foo_del.argtypes = [ctypes.POINTER(ctypes.c_char)]

关于python - 使用 ctypes 和线程时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37953055/

相关文章:

java - 用Java实现生产者消费者

Python 多处理 - 进程之间的管道通信

python - 将颜色图添加到图表中

python - 为什么 tensorflow reshape 数组超出范围

c - 在 C 中比较指针和整数可以吗?

java - 同时运行两个线程,然后在它们之间进行通信

python - 通过 ubuntu 应用程序运行的 Matplotlib 弃用警告

c - 初始化二维数组

c - 如何使以下双线性插值代码更高效?

c# - 中止使用 Delegate.BeginInvoke 启动的线程