From 3a53d8667a523a9e451398d68126d6567b5bf1ca Mon Sep 17 00:00:00 2001 From: James Bowman Date: Tue, 25 Jan 2011 01:05:12 +0000 Subject: [PATCH] #489, leaking cvarrmat test and fix --- modules/python/cv.cpp | 13 ++++++++++++- tests/python/leak4.py | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/python/leak4.py diff --git a/modules/python/cv.cpp b/modules/python/cv.cpp index 80df480a9..ffd35c3d8 100644 --- a/modules/python/cv.cpp +++ b/modules/python/cv.cpp @@ -1734,11 +1734,21 @@ static int convert_to_pts_npts_contours(PyObject *o, pts_npts_contours *dst, con return 1; } -struct cvarrseq { +class cvarrseq { +public: union { CvSeq *seq; CvArr *mat; }; + int freemat; + cvarrseq() { + freemat = false; + } + ~cvarrseq() { + if (freemat) { + cvReleaseMat((CvMat**)&mat); + } + } }; static int is_convertible_to_mat(PyObject *o) @@ -1782,6 +1792,7 @@ static int convert_to_cvarrseq(PyObject *o, cvarrseq *dst, const char *name = "n } assert(size != -1); CvMat *mt = cvCreateMat((int)PySequence_Fast_GET_SIZE(fi), 1, CV_32SC(size)); + dst->freemat = true; // dealloc this mat when done for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(fi); i++) { PyObject *e = PySequence_Fast_GET_ITEM(fi, i); PyObject *fe = PySequence_Fast(e, name); diff --git a/tests/python/leak4.py b/tests/python/leak4.py new file mode 100644 index 000000000..326820052 --- /dev/null +++ b/tests/python/leak4.py @@ -0,0 +1,9 @@ +import cv +import math +import time + +N=50000 +print "leak4" +while True: + seq=list((i*1., i*1.) for i in range(N)) + cv.Moments(seq)