Changeset 4526

Show
Ignore:
Timestamp:
01/19/11 12:47:10 (3 years ago)
Author:
jamesb
Message:

#674, fix leaks in CreateHist?

Location:
trunk/opencv
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/opencv/modules/python/cv.cpp

    r4519 r4526  
    29062906#endif 
    29072907 
     2908class ranges { 
     2909public: 
     2910  Py_ssize_t len; 
     2911  float **rr; 
     2912  ranges() { 
     2913    len = 0; 
     2914    rr = NULL; 
     2915  } 
     2916  int fromobj(PyObject *o, const char *name = "no_name") { 
     2917    PyObject *fi = PySequence_Fast(o, name); 
     2918    if (fi == NULL) 
     2919      return 0; 
     2920    len = PySequence_Fast_GET_SIZE(fi); 
     2921    rr = new float*[len]; 
     2922    for (Py_ssize_t i = 0; i < len; i++) { 
     2923      PyObject *item = PySequence_Fast_GET_ITEM(fi, i); 
     2924      floats ff; 
     2925      if (!convert_to_floats(item, &ff)) 
     2926        return 0; 
     2927      rr[i] = ff.f; 
     2928    } 
     2929    Py_DECREF(fi); 
     2930    return 1; 
     2931  } 
     2932  ~ranges() { 
     2933    for (Py_ssize_t i = 0; i < len; i++) 
     2934      delete rr[i]; 
     2935   delete rr; 
     2936  } 
     2937}; 
     2938 
     2939static int ranges_converter(PyObject *o, ranges* dst) 
     2940{ 
     2941  return dst->fromobj(o); 
     2942} 
     2943 
    29082944static PyObject *pycvCreateHist(PyObject *self, PyObject *args, PyObject *kw) 
    29092945{ 
     
    29112947  PyObject *dims; 
    29122948  int type; 
    2913   float **ranges = NULL; 
    29142949  int uniform = 1; 
    2915  
    2916   if (!PyArg_ParseTupleAndKeywords(args, kw, "Oi|O&i", (char**)keywords, &dims, &type, convert_to_floatPTRPTR, (void*)&ranges, &uniform)) { 
     2950  ranges r; 
     2951  if (!PyArg_ParseTupleAndKeywords(args, kw, "Oi|O&i", (char**)keywords, &dims, &type, ranges_converter, (void*)&r, &uniform)) { 
    29172952    return NULL; 
    29182953  } 
     
    29282963    return NULL; 
    29292964 
    2930   ERRWRAP(cvSetHistBinRanges(&(h->h), ranges, uniform)); 
     2965  ERRWRAP(cvSetHistBinRanges(&(h->h), r.rr, uniform)); 
    29312966 
    29322967  return (PyObject*)h; 
  • trunk/opencv/tests/python/leak3.py

    r4518 r4526  
    11import cv 
     2import math 
     3import time 
    24 
    35while True: 
    4   cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1) 
     6    h = cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1)