Skip to main content
 首页 » 编程设计

python之用 C 编写 Python 扩展,出现奇怪的内存错误

2025年05月04日8Renyi-Fan

我有一个用 C 编写的名为 pyquat.Quat 的 Python 类,它包含与另一个 Quat 相乘的方法,返回一个 NumPy 数组 (Quat#to_matrix()) 并用于打印表示。

有时当我调用这些方法时,我会得到一个SIGSEGV。但是,当我在 GDB 中运行该程序时,从未发生过段错误。

虽然我似乎无法追踪问题所在,但我看到了一些奇怪的内存被覆盖的情况(显然)。例如,这是我的脚本和输出:

>>> from pyquat import Quat 
>>> z = Quat(4,3,2,1) * Quat(1,2,3,4) 
>>> z 
Quat{{-12, 6, 24, 12}} 
>>> z.normalize() 
Quat{{-0.40000000000000002, 0.20000000000000001, 0.80000000000000004, 0.40000000000000002}} 
>>> m = z.to_matrix() 
>>> m 
array([[-0.6 ,  0.  ,  0.8 ], 
       [ 0.64,  0.6 ,  0.48], 
       [-0.48,  0.8 , -0.36]]) 
>>> z 
'exc_traceback' 

我一直在做很多搜索,但我不知道为什么它会在这里用字符串覆盖调用者。

有问题的功能是:

static PyObject* pyquat_Quat_to_matrix(PyObject* self) { 
  npy_intp dims[2] = {3,3}; 
 
  pyquat_Quat* q = (pyquat_Quat*)(self); 
 
  PyArrayObject* ary  = (PyArrayObject*)PyArray_SimpleNew(2, dims, NPY_DOUBLE); 
  double* T = (double*)ary->data; 
 
  T[0] = 1.0 - 2.0 * (q->v[2] * q->v[2] + q->v[1] * q->v[1]);  
  T[1] =       2.0 * (q->v[1] * q->v[0] +    q->s * q->v[2]); 
  T[2] =       2.0 * (q->v[2] * q->v[0] -    q->s * q->v[1]); 
  T[3] =       2.0 * (q->v[1] * q->v[0] -    q->s * q->v[2]); 
  T[4] = 1.0 - 2.0 * (q->v[2] * q->v[2] + q->v[0] * q->v[0]); 
  T[5] =       2.0 * (q->v[2] * q->v[1] +    q->s * q->v[0]); 
  T[6] =       2.0 * (q->v[2] * q->v[0] +    q->s * q->v[1]); 
  T[7] =       2.0 * (q->v[2] * q->v[1] -    q->s * q->v[0]); 
  T[8] = 1.0 - 2.0 * (q->v[1] * q->v[1] + q->v[0] * q->v[0]); 
 
  return PyArray_Return(ary); 
} 

我的类型看起来像

typedef struct { 
  PyObject_HEAD 
 
  /* Type-specific fields go here */ 
  double s;     // scalar component 
  double v[3];  // vector components 
} pyquat_Quat; 

我相当确定我正确地包含了 numpy,并创建了我的模块和我的类:

/* Initialize the pyquat module and add pyquat.Quat to it.  */ 
PyMODINIT_FUNC initpyquat(void) { 
  PyObject* m; 
 
  pyquat_QuatType.tp_new = PyType_GenericNew; 
  if (PyType_Ready(&pyquat_QuatType) < 0) 
    return; 
 
  // Define the pyquat module. 
  m = Py_InitModule3("pyquat", pyquat_methods, 
         "Quaternion module with fast unit (right) quaternion math written in C."); 
 
  // Import NumPy to prevent a segfault when we call a function that uses NumPy API. 
  import_array(); 
 
  // Create the Quat class in the pyquat module. 
  Py_INCREF(&pyquat_QuatType); 
  PyModule_AddObject(m, "Quat", (PyObject *)&pyquat_QuatType); 
} 
 
 
static int pyquat_Quat_init(pyquat_Quat* self, PyObject* args) { 
 
  double scalar, vx, vy, vz; 
 
  if (!PyArg_ParseTuple(args, "dddd", &scalar, &vx, &vy, &vz)) 
    return -1; 
 
  // Read the scalar and vector components of the quaternion. 
  self->s = scalar; 
  self->v[0] = vx; 
  self->v[1] = vy; 
  self->v[2] = vz; 
 
  return 0; 
} 

我的 repr 方法看起来也很典型,但我注意到这个方法偶尔也会导致 obj 被覆盖。

static PyObject* pyquat_Quat_repr(PyObject* obj) { 
  pyquat_Quat* self = (pyquat_Quat*)(obj); 
  return PyString_FromFormat("Quat{{\%s, \%s, \%s, \%s}}",  
                             PyOS_double_to_string(self->s, 'g', 17, 0, NULL), 
                             PyOS_double_to_string(self->v[0], 'g', 17, 0, NULL), 
                             PyOS_double_to_string(self->v[1], 'g', 17, 0, NULL), 
                             PyOS_double_to_string(self->v[2], 'g', 17, 0, NULL)); 
} 
 
 
static PyObject * pyquat_Quat_mul(PyObject* self, PyObject* arg) { 
 
  // Expects the one argument to be a pyquat_Quat 
  if (!PyObject_IsInstance(arg, (PyObject*)&pyquat_QuatType)) { 
    Py_DECREF(arg); 
    PyErr_SetString(PyExc_IOError, "expected quaternion"); 
    return NULL; 
  } 
 
  pyquat_Quat* rhs    = (pyquat_Quat*)(arg); 
  pyquat_Quat* lhs    = (pyquat_Quat*)(self); 
  pyquat_Quat* result = (pyquat_Quat *)Py_TYPE(self)->tp_alloc(Py_TYPE(self), 0); 
 
  result->s    = lhs->s * rhs->s - (lhs->v[0] * rhs->v[0] + lhs->v[1] * rhs->v[1] + lhs->v[2] * rhs->v[2]); 
  result->v[0] = lhs->s * rhs->v[0] + rhs->s * lhs->v[0] - (lhs->v[1] * rhs->v[2] - lhs->v[2] * rhs->v[1]); 
  result->v[1] = lhs->s * rhs->v[1] + rhs->s * lhs->v[1] - (lhs->v[2] * rhs->v[0] - lhs->v[0] * rhs->v[2]); 
  result->v[2] = lhs->s * rhs->v[2] + rhs->s * lhs->v[2] - (lhs->v[0] * rhs->v[1] - lhs->v[1] * rhs->v[0]); 
 
  return (PyObject*)(result); 
} 

这是一个我有些担心的问题。它正在返回 self — 但为什么同样返回一个对象的乘法函数不也导致 Python 回显该对象的 repr?

编辑:经过一些调查,问题似乎只在我调用就地规范化例程时发生。我需要在这里增加或减少一些引用计数器吗?

static PyObject* pyquat_Quat_inplace_normalize(PyObject* self) { 
 
  pyquat_Quat* q = (pyquat_Quat*)(self); 
 
  double q_mag = sqrt(q->s * q->s + q->v[0] * q->v[0] + q->v[1] * q->v[1] + q->v[2] * q->v[2]); 
  if (q_mag > PYQUAT_QUAT_SMALL) q_mag = 1.0 / q_mag; 
  else                           q_mag = 0.0; 
 
  q->s    *= q_mag; 
  q->v[0] *= q_mag; 
  q->v[1] *= q_mag; 
  q->v[2] *= q_mag; 
 
  return self; 
} 

这是乘法函数:

static PyObject * pyquat_Quat_mul(PyObject* self, PyObject* arg) { 
 
  // Expects the one argument to be a pyquat_Quat 
  if (!PyObject_IsInstance(arg, (PyObject*)&pyquat_QuatType)) { 
    Py_DECREF(arg); 
    PyErr_SetString(PyExc_IOError, "expected quaternion"); 
    return NULL; 
  } 
 
  pyquat_Quat* rhs    = (pyquat_Quat*)(arg); 
  pyquat_Quat* lhs    = (pyquat_Quat*)(self); 
  pyquat_Quat* result = (pyquat_Quat *)Py_TYPE(self)->tp_alloc(Py_TYPE(self), 0); 
 
  result->s    = lhs->s * rhs->s - (lhs->v[0] * rhs->v[0] + lhs->v[1] * rhs->v[1] + lhs->v[2] * rhs->v[2]); 
  result->v[0] = lhs->s * rhs->v[0] + rhs->s * lhs->v[0] - (lhs->v[1] * rhs->v[2] - lhs->v[2] * rhs->v[1]); 
  result->v[1] = lhs->s * rhs->v[1] + rhs->s * lhs->v[1] - (lhs->v[2] * rhs->v[0] - lhs->v[0] * rhs->v[2]); 
  result->v[2] = lhs->s * rhs->v[2] + rhs->s * lhs->v[2] - (lhs->v[0] * rhs->v[1] - lhs->v[1] * rhs->v[0]); 
 
  return (PyObject*)(result); 
} 

有没有人对我如何更有效地调试它有任何建议,或者看看可能是什么问题?

请您参考如下方法:

你说通过 Py_INCREF 在从你的方法返回一些值之前,问题似乎消失了。

受你发现的启发,我发现了问题:

static PyObject * pyquat_Quat_mul(PyObject* self, PyObject* arg) { 
  // Expects the one argument to be a pyquat_Quat 
  if (!PyObject_IsInstance(arg, (PyObject*)&pyquat_QuatType)) { 
    Py_DECREF(arg); 

您正在 Py_DECREFing 参数。你不应该那样做!仅当对象存储在某处时,才应增加和减少引用计数。