7

我在这里问是因为到目前为止我还没有从 OpenCV 开发人员那里得到任何帮助。我将问题简化为一个非常简单的测试用例,因此可能有 CPython 背景的任何人都可以在这里提供帮助。

此 C 代码不会泄漏:

int main() {
    while(true) {
        int hist_size[] = {40};
        float range[] = {0.0f,255.0f};
        float* ranges[] = {range};
        CvHistogram* hist = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1);
        cvReleaseHist(&hist);
    }
}

此 Python 代码确实泄漏:

while True: cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1)

我搜索了 CPython 代码(OpenCVs 当前的 SVN 主干代码),发现了这个:

struct cvhistogram_t {
  PyObject_HEAD
  CvHistogram h;
  PyObject *bins;
};

...

/* cvhistogram */

static void cvhistogram_dealloc(PyObject *self)
{
  cvhistogram_t *cvh = (cvhistogram_t*)self;
  Py_DECREF(cvh->bins);
  PyObject_Del(self);
}

static PyTypeObject cvhistogram_Type = {
  PyObject_HEAD_INIT(&PyType_Type)
  0,                                      /*size*/
  MODULESTR".cvhistogram",                /*name*/
  sizeof(cvhistogram_t),                  /*basicsize*/
};

static PyObject *cvhistogram_getbins(cvhistogram_t *cvh)
{
  Py_INCREF(cvh->bins);
  return cvh->bins;
}

static PyGetSetDef cvhistogram_getseters[] = {
  {(char*)"bins", (getter)cvhistogram_getbins, (setter)NULL, (char*)"bins", NULL},
  {NULL}  /* Sentinel */
};

static void cvhistogram_specials(void)
{
  cvhistogram_Type.tp_dealloc = cvhistogram_dealloc;
  cvhistogram_Type.tp_getset = cvhistogram_getseters;
}

...

static PyObject *pycvCreateHist(PyObject *self, PyObject *args, PyObject *kw)
{
  const char *keywords[] = { "dims", "type", "ranges", "uniform", NULL };
  PyObject *dims;
  int type;
  float **ranges = NULL;
  int uniform = 1;

  if (!PyArg_ParseTupleAndKeywords(args, kw, "Oi|O&i", (char**)keywords, &dims, &type, convert_to_floatPTRPTR, (void*)&ranges, &uniform)) {
    return NULL;
  }
  cvhistogram_t *h = PyObject_NEW(cvhistogram_t, &cvhistogram_Type);
  args = Py_BuildValue("Oi", dims, CV_32FC1);
  h->bins = pycvCreateMatND(self, args);
  Py_DECREF(args);
  if (h->bins == NULL) {
    return NULL;
  }
  h->h.type = CV_HIST_MAGIC_VAL;
  if (!convert_to_CvArr(h->bins, &(h->h.bins), "bins"))
    return NULL;

  ERRWRAP(cvSetHistBinRanges(&(h->h), ranges, uniform));

  return (PyObject*)h;
}

从 OpenCV C 头文件中:

typedef struct CvHistogram
{
    int     type;
    CvArr*  bins;
    float   thresh[CV_MAX_DIM][2];  /* For uniform histograms.                      */
    float** thresh2;                /* For non-uniform histograms.                  */
    CvMatND mat;                    /* Embedded matrix header for array histograms. */
}
CvHistogram;

我并不完全了解所有内容,因为我以前从未使用过 Python 的 C 接口。但可能我正在寻找的错误在这段代码中的某个地方。

我对吗?或者我应该在哪里搜索错误?我将如何解决它?

(对于看过此问题早期版本的人的注意事项:我查看了错误的代码。他们的 SWIG 接口已被弃用并且不再使用(但代码仍然存在于 SVN 中,这就是我混淆它的原因。所以不要t查看interfaces/swig,此代码旧且未使用。当前代码位于modules/python。)


上游错误报告:OpenCV Python CreateHist 中的 memleak

4

2 回答 2

2

它已被修复。

由 jamesb 于 3 周前更改

  • 状态从已接受更改为已关闭
  • 分辨率设置为固定

已在r4526中修复

范围参数没有被释放,并且范围上的迭代器没有被 DECREF'ed。回归现在通过了,原始循环没有泄漏。

于 2011-02-09T11:42:48.727 回答
0

我认为您有垃圾收集问题,因为您永远不会离开循环。

这是否更符合预期?

while True: 
    cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1)
    cv = None
于 2011-01-21T22:42:54.260 回答