将 python 对象传递到用 Cython 包装的类时出现 Seg 错误
Seg fault when passing python object into class wrapped with Cython
我用Cython包装了许多C++类,他们设法编译了。但是,当我尝试使用 python 级别的模块时,我遇到了分段错误 (11),所以我想知道我包装的内容是否正确。"Foo.h"中的A是一个抽象类。
来源.pyx
cdef extern from "Foo.h":
cdef cppclass A:
A(MPI_comm comm, int x)
cdef extern from "Foo1.h":
cdef cppclass B:
B(A* obj, int y)
cdef class pyA:
cdef A *thisptrA
def __cinit__(self,MPI.Comm _comm, int x):
pass
def __dealloc__(self):
pass
cdef class pyB:
cdef B* thisptrB
cdef pyA obj
def __cinit__(self, pyA obj, int y):
self.thisptrB = new B(obj.thisptrA, y)
def __dealloc__(self):
del self.thisptrB
testscript.py
import pyA, pyB
class C(pyA):
def __init__(self, comm, x):
self.x = x
comm = MPI.COMM_WORLD
x = 2
y = 24
Aobj = C(comm, x)
Bobj = pyB(Aobj, y)
似乎每当我尝试初始化pyB时,语句都会发生分段错误
self.thisptrB = newB(obj.thisptrA, y)
有人知道我哪里出错了吗?
你不希望在pyA
中分配thisptrA
。因此,它指向的东西要么是空的,要么是随机的(我不确定哪个是 Cython)。
导致分段错误的确切原因可能隐藏在构造函数中 B
,您不会让我们做空。
还需要考虑的另一件事:您可能希望确保A
的生存期超过(我假设)持有指向它的指针的B
的生存期。一种方法是在PyB
中保留对PyA
的引用(即在__init__
内,以便PyB
放置self._held_A = obj
)
相关文章:
- C++ - 将元素按升序插入数组的 SEG 错误
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 字符串上的合并排序上的 Seg 错误
- 导致 Seg 错误的 SingleLinkedList 的析构函数
- g++ 9.2.1 (Linux) 会导致 seg 错误,但 Windows 上的代码块不会
- 函数抛出seg错误,我不知道为什么
- Microsoft Visual Studio 2019交叉编译CMake库 - 有效,但存在SEG错误
- 由于相同的文件名/类名存在于不同的SO中而导致的SEG错误
- 尝试访问 3D 矢量中的元素时出现 Seg 错误
- 垫子对象数组太大会导致 Seg 错误
- 为什么push_back成矢量<矢量<int>>导致 seg 错误?
- 在向量中存储字符串存在 SEG 错误
- 更改本地类名时出现 seg 错误
- 使用 ->GetString( " ") 时出现 Seg 错误,它位于单独的类中
- 将字符数组转换为 std::string 以传递到 std::bitset seg 错误
- 字符和 int 连接何时会导致 seg 错误?
- 静态数组溢出堆栈(seg 错误 11)仅在类定义内部时,否则不会..?
- .EOF 在读取文件时导致 Seg 错误:FIXXED
- 在 qt5 中使用 QSharedPointer 时出现 Seg 错误
- 在c++和Linux中查找总线错误/Seg故障的问题