From d3462dfcbaa8f8b84a4d1ad82a4659a0b2dd3b32 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Wed, 19 Jan 2011 20:47:10 +0000 Subject: [PATCH] #674, fix leaks in CreateHist --- modules/python/cv.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- tests/python/leak3.py | 4 +++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/modules/python/cv.cpp b/modules/python/cv.cpp index 6cbe1d9..80df480 100644 --- a/modules/python/cv.cpp +++ b/modules/python/cv.cpp @@ -2905,15 +2905,50 @@ static PyObject *fromarray(PyObject *o, int allowND) } #endif +class ranges { +public: + Py_ssize_t len; + float **rr; + ranges() { + len = 0; + rr = NULL; + } + int fromobj(PyObject *o, const char *name = "no_name") { + PyObject *fi = PySequence_Fast(o, name); + if (fi == NULL) + return 0; + len = PySequence_Fast_GET_SIZE(fi); + rr = new float*[len]; + for (Py_ssize_t i = 0; i < len; i++) { + PyObject *item = PySequence_Fast_GET_ITEM(fi, i); + floats ff; + if (!convert_to_floats(item, &ff)) + return 0; + rr[i] = ff.f; + } + Py_DECREF(fi); + return 1; + } + ~ranges() { + for (Py_ssize_t i = 0; i < len; i++) + delete rr[i]; + delete rr; + } +}; + +static int ranges_converter(PyObject *o, ranges* dst) +{ + return dst->fromobj(o); +} + 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)) { + ranges r; + if (!PyArg_ParseTupleAndKeywords(args, kw, "Oi|O&i", (char**)keywords, &dims, &type, ranges_converter, (void*)&r, &uniform)) { return NULL; } cvhistogram_t *h = PyObject_NEW(cvhistogram_t, &cvhistogram_Type); @@ -2927,7 +2962,7 @@ static PyObject *pycvCreateHist(PyObject *self, PyObject *args, PyObject *kw) if (!convert_to_CvArr(h->bins, &(h->h.bins), "bins")) return NULL; - ERRWRAP(cvSetHistBinRanges(&(h->h), ranges, uniform)); + ERRWRAP(cvSetHistBinRanges(&(h->h), r.rr, uniform)); return (PyObject*)h; } diff --git a/tests/python/leak3.py b/tests/python/leak3.py index b5579f3..c12364e 100644 --- a/tests/python/leak3.py +++ b/tests/python/leak3.py @@ -1,4 +1,6 @@ import cv +import math +import time while True: - cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1) + h = cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1) -- 2.7.4