Python:ctypes + C malloc错误. C内存问题还是Python/ctypes问题? [英] Python: ctypes + C malloc error. C memory problem or Python/ctypes problem?

查看:193
本文介绍了Python:ctypes + C malloc错误. C内存问题还是Python/ctypes问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人.我在使用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

python(79698)malloc: *对象0x15627ac08的错误:已释放对象的校验和不正确>-对象在释放后可能已被修改. * 在malloc_error_break中设置一个断点以进行调试 中止

python(79698) malloc: * error for object 0x15627ac08: incorrect checksum for freed object >- object was probably modified after being freed. * set a breakpoint in malloc_error_break to debug Abort

python(76110,0x7fff70062ca0)malloc: *对象0x7d807e1078907df的错误:正在释放的指针未分配 * 在malloc_error_break中设置一个断点以进行调试 中止

python(76110,0x7fff70062ca0) malloc: * error for object 0x7d807e1078907df: pointer being >freed was not allocated * set a breakpoint in malloc_error_break to debug Abort

取决于我如何编写以下代码的"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问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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