问题描述
每个人.我在使用ctypes和C代码时遇到内存分配错误.我想知道内存问题是在C内部还是由ctypes的不正确使用引起.内存错误是
everyone. I'm having a memory allocation error using ctypes and a C code. I'm wondering if the memory problem is inside of C, or caused by an improper use of ctypes. The memory error is
或
取决于我如何编写以下代码的"free_some_memory"部分.我不会在此处发布一堆C代码,但是假设我正确编写了C代码,我的ctypes接口看起来正确吗?
depending on how I write the "free_some_memory" part of the code below. I won't post a bunch of C code here, but assuming I've written the C code correctly, does my ctypes interface look correct?
对于给定版本的"free_some_memory",我遇到的故障并不总是在脚本中的同一位置发生. 我的C代码内存管理是否写得不好,以至于Python内存管理可能会(或可能不会)束缚(取消分配)?
Also, the failures I'm experiencing don't always happen in the same place in my scripts, for a given version of "free_some_memory". Is it possible that my C code memory management is written poorly such that Python memory management may or may not bungle up (de)allocations?
Dunno(如果这很重要),但是我正在Mac上使用Snow Leopard.
您能提供的任何帮助将不胜感激.
Dunno if this is important, but I'm working on a Mac, Snow Leopard.
Any help you can offer would be very appreciated.
最好,jkmacc
Best,jkmacc
我的C代码如下:
/**** cfunction.c ****/
int cfun (double *y, char datafile[1024], int byteoffset, int num )
{
allocate_some_memory;
do_stuff;
if ( error_condition )
{
free_some_memory;
return ( -1 );
}
fill_y_here;
free_some_memory;
return ( 0 );
}
我的ctypes包装器看起来像:
My ctypes wrapper looks like:
#pyfunction.py
import ctypes as C
import numpy as np
lib = C.CDLL('mylib.dylib')
def pyfun(DATAFILE, BYTEOFFSET, NUM):
"""
DATAFILE: a string file name
BYTEOFFSET: an integer
NUM: an integer
"""
#return integer success/failure flag
lib.cfun.restype = C.c_int
#array memory to be filled inside of C
Y = np.empty(NUM,dtype='double',order='C')
cDATAFILE = C.create_string_buffer(DATAFILE,1024)
cBYTEOFFSET = C.c_int(int(BYTEOFFSET))
cNUM = C.c_int(int(NUM))
flag = lib.cfun(Y.ctypes.data_as(C.POINTER(C.c_double)), \
cDATAFILE,cBYTEOFFSET,cNUM)
if flag == -1:
print("Something went wrong.")
return -1
else:
return Y
推荐答案
我遇到了这个问题,这是我所发现的:
I had this problem and here is what I figured out:
如果使用ctypes映射某些东西然后释放它,则在销毁ctypes对象时会出现错误.因此,在调用free()之前,需要确保python代码中没有剩余的引用.
If you map something with ctypes and then free it you will get an error when the ctypes object is destroyed. So, before you call free(), you need to make sure that there are no remaining references to it in the python code.
因此,您的C代码类似于:
So, your C code will be like:
char* p;
char* allocate() {
p = asprintf("hello world");
return(p)
}
void freep() {
free(p);
}
您的Python代码如下:
And your Python code will be like:
allocate = clib.allocate
allocate.restype = c_char_p()
p = allocate()
print p
# This is the key:
del(p)
clib.freep()
如果您想看到错误,只需省略del(p)
If you want to see an error, just omit del(p)
这篇关于Python:ctypes + C malloc错误. C内存问题还是Python/ctypes问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!