python:在cygwin上使用ctypes时的sigsegv

时间:2015-07-16 05:36:32

标签: python c cygwin ctypes sigsegv

我正在尝试使用ctypes模块在python中编译和使用c库。该库在Linux机器上运行良好,但在Cygwin64上抛出了SIGSEGV。

import ctypes
import numpy as np
import pdb

xbry = np.array([0.9, 0.1, 0.2, 0.9])
ybry = np.array([0.9, 0.9, 0.1, 0.1])
beta = np.array([1.0, 1.0, 1.0, 1.0])

nx = 30
ny = 30
ul_idx = 0

nnodes=14
precision=1.0e-12
nppe=3
newton=True
thin=True
checksimplepoly=True
verbose=True

_libgridgen = np.ctypeslib.load_library('libgridgen.dll', '/home/Nikhil/python/octant/gridgen')

print _libgridgen

_libgridgen.gridgen_generategrid2.restype = ctypes.c_void_p
_libgridgen.gridnodes_getx.restype = ctypes.POINTER(ctypes.POINTER(ctypes.c_double))
_libgridgen.gridnodes_gety.restype = ctypes.POINTER(ctypes.POINTER(ctypes.c_double))
_libgridgen.gridnodes_getnce1.restype = ctypes.c_int
_libgridgen.gridnodes_getnce2.restype = ctypes.c_int

_libgridgen.gridnodes_getnx.restype = ctypes.c_int
_libgridgen.gridnodes_getny.restype = ctypes.c_int

_libgridgen.gridmap_build.restype = ctypes.c_void_p


nbry = len(xbry)

nsigmas = ctypes.c_int(0)
sigmas = ctypes.c_void_p(0)
nrect = ctypes.c_int(0)
xrect =  ctypes.c_void_p(0)
yrect = ctypes.c_void_p(0)

ngrid = ctypes.c_int(0)
xgrid = ctypes.POINTER(ctypes.c_double)()
ygrid = ctypes.POINTER(ctypes.c_double)()

_gn = _libgridgen.gridgen_generategrid2(
 ctypes.c_int(nbry), 
 (ctypes.c_double * nbry)(*xbry), 
 (ctypes.c_double * nbry)(*ybry), 
 (ctypes.c_double * nbry)(*beta),
 ctypes.c_int(ul_idx), 
 ctypes.c_int(nx), 
 ctypes.c_int(ny), 
 ngrid, 
 xgrid, 
 ygrid,
 ctypes.c_int(nnodes), 
 ctypes.c_int(newton), 
 ctypes.c_double(precision),
 ctypes.c_int(checksimplepoly), 
 ctypes.c_int(thin), 
 ctypes.c_int(nppe),
 ctypes.c_int(verbose),
 ctypes.byref(nsigmas), 
 ctypes.byref(sigmas), 
 ctypes.byref(nrect),
 ctypes.byref(xrect), 
 ctypes.byref(yrect) )


 print 'run getx'
 x = _libgridgen.gridnodes_getx(_gn)

 print 'reshape result.'
 x = np.asarray([x[0][i] for i in range(ny*nx)])
 x.shape = (ny, nx)


 print 'run gety'
 y = _libgridgen.gridnodes_gety(_gn)

 print 'reshape result.'
 y = np.asarray([y[0][i] for i in range(ny*nx)])
 y.shape = (ny, nx)

回溯

Program received signal SIGSEGV, Segmentation fault.
0x0000000542596595 in gridnodes_getx (gn=0x45ab50) at gridnodes.c:789
789         return gn->gx;
(gdb) backtrace
0  0x0000000542596595 in gridnodes_getx (gn=0x45ab50) at gridnodes.c:789

这是c代码回溯指的是

int gridnodes_getnx(gridnodes* gn)
{
    return gn->nx;
}

int gridnodes_getny(gridnodes* gn)
{
    return gn->ny;
}

double** gridnodes_getx(gridnodes* gn)
{
    return gn->gx;
}

double** gridnodes_gety(gridnodes* gn)
{
    return gn->gy;
}

如果有人可以帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我设法通过使用类

来解决问题
class gridnodes (ctypes.Structure):
    _fields_ = [
    ('nx',ctypes.c_int), \
    ('ny',ctypes.c_int), \
    ('gx', ctypes.c_double), \
    ('gy', ctypes.c_double)]

并替换

_libgridgen.gridgen_generategrid2.restype = ctypes.c_void_p

通过

_libgridgen.gridgen_generategrid2.restype = ctypes.POINTER(gridnodes)

我也尝试了@eryksun的建议,这也有效。