确定“不正确的校验和释放对象”,使用用Cython的C++的支持后,我得到一个奇怪的错误,因为我觉得manual advises(使用__cinit__
分配堆内存和__dealloc__
再次释放它。用Cython扩展类:后__dealloc__方法
我有一个C++类calloc
和malloc
小号一些RAM在其构造和free
的IT在析构函数。如果有必要,我会张贴此,但错误不是从C++来到,和new
和delete
电话做工精细,所以我只给你的用Cython现在:
cdef extern from "../clibs/adjacency.hpp":
cdef cppclass Adjacency:
int* _bv
void** _meta
unsigned int length
Adjacency(int graph_order, int meta_on)
int get(int i, int j)
void set(int i, int j, int on, void* meta)
void reset_to_zero()
void edges_iter(void* global_meta, void (*callback)(void*, int, int, void*))
cdef class Graph:
cdef Adjacency* adj
def __init__(self, graph_order):
print "__init__"
def __cinit__(self, graph_order, *args, **kwargs):
print "__cinit__"
print "allocating, no meta."
self.adj = new Adjacency(<int>graph_order, 0)
def __dealloc__(self):
print "__dealloc__"
del self.adj
当我测试,我得到以下几点:
In [1]: import graph
In [2]: g = graph.Graph(302)
__cinit__
allocating, no meta.
__init__
In [3]: ^D
Do you really want to exit ([y]/n)?
__dealloc__
Python(7389,0x7fff70f9acc0) malloc: *** error for object 0x100defa08: incorrect
checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap
我有时也得到了段错误,而不是一个不正确的校验和。
我破解打开用Cython生成的C++文件,并添加周围的delete
调用以下:
/* "graph.pyx":75
* def __dealloc__(self):
* print "__dealloc__"
* del self.adj # <<<<<<<<<<<<<<
*
* def __init__(self, graph_order):
*/
printf("about to delete adj:\n");
delete __pyx_v_self->adj;
printf("just deleted adj!\n");
,我们可以清楚地看到,在呼叫我到delete
运行得细致:
In [1]: import graph
In [2]: g = graph.Graph(302)
__cinit__
allocating, no meta.
__init__
In [3]: ^D
Do you really want to exit ([y]/n)?
__dealloc__
about to delete adj:
just deleted adj!
Python(7389,0x7fff70f9acc0) malloc: *** error for object 0x100defa08: incorrect
checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap
而且Python在之后会引起发脾气我已经完成整理,正如我在Cython手册中所告诉的那样。
我也试过设置self.adj = NULL
在__dealloc__
,以防Python试图在我的对象内部看到,但没有帮助。
任何想法我做错了什么?