3

我正在尝试在 Python 中创建一个具有特殊类型的缓冲区,以将其发送到用 CFFI 包装的 C 函数。

在C中,我有类似的东西:

typedef unsigned char UINT8;
typedef UINT8* PUINT8;

然后,在 Python 中,上面的行在ffi.cdef()我的缓冲区实例化代码中,如下所示:

nb_buffer = 8
buffer_size = 42

buffers = ffi.new( "PUINT8[]", nb_buffer )

for i in range( nb_buffer ):
    tmp_buffer = ffi.buffer( ffi.new( "UINT8[]", 1 ), buffer_size )
    buffers[ i ] = ffi.cast( "PUINT8", ffi.from_buffer( tmp_buffer ) )

c.function( buffers )

C 函数接收 UINT8**。

并且...在 C 函数中以进一步的分段错误结束。

所以我print buffers[ i ]使用后ffi.cast

<cdata 'unsigned char *' 0x2cbaca0>
<cdata 'unsigned char *' 0x2cbacd8>
<cdata 'unsigned char *' 0x2cbaca0>
<cdata 'unsigned char *' 0x2cbacd8>
<cdata 'unsigned char *' 0x2cbaca0>
<cdata 'unsigned char *' 0x2cbacd8>
<cdata 'unsigned char *' 0x2cbaca0>
<cdata 'unsigned char *' 0x2cbacd8>

我错过了什么?缓冲区一覆盖就被垃圾收集tmp_buffer吗?

4

2 回答 2

3

怎么样:

buffers = ffi.new( "PUINT8[]", nb_buffer )
keepalive = []

for i in range( nb_buffer ):
    p = ffi.new("UINT8[]", buffer_size)
    keepalive.append(p)
    buffers[i] = p

c.function( buffers )

# keepalive stays alive at least until here
于 2016-05-14T12:32:18.537 回答
0

使用列表理解:

keepalive = [ ffi.new( "UINT8[]", buffer_size ) for i in range( nb_buffer ) ]
buffers = ffi.new( "PUINT8[]", keepalive )

c.functions( buffers )

# keepalive stays alive at least until here
于 2016-08-02T22:53:00.843 回答