From e1e243588e73aa96a458f8f585f4ed055fc685d4 Mon Sep 17 00:00:00 2001 From: Sancho McCann Date: Wed, 11 Jun 2014 17:25:22 -0700 Subject: [PATCH] Bugfix: Memory leak in deletion of er_stack nodes of ERFilter. --- modules/objdetect/src/erfilter.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/objdetect/src/erfilter.cpp b/modules/objdetect/src/erfilter.cpp index e942094b1d..6651a00933 100644 --- a/modules/objdetect/src/erfilter.cpp +++ b/modules/objdetect/src/erfilter.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include +#include #if defined _MSC_VER && _MSC_VER == 1500 typedef int int_fast32_t; @@ -57,6 +58,27 @@ using namespace std; namespace cv { +// Deletes a tree of ERStat regions starting at root. Used only +// internally to this implementation. +static void deleteERStatTree(ERStat* root) { + queue to_delete; + to_delete.push(root); + while (!to_delete.empty()) { + ERStat* n = to_delete.front(); + to_delete.pop(); + ERStat* c = n->child; + if (c != NULL) { + to_delete.push(c); + ERStat* sibling = c->next; + while (sibling != NULL) { + to_delete.push(sibling); + sibling = sibling->next; + } + } + delete n; + } +} + ERStat::ERStat(int init_level, int init_pixel, int init_x, int init_y) : pixel(init_pixel), level(init_level), area(0), perimeter(0), euler(0), probability(1.0), parent(0), child(0), next(0), prev(0), local_maxima(0), @@ -497,7 +519,7 @@ void ERFilterNM::er_tree_extract( InputArray image ) delete(stat->crossings); stat->crossings = NULL; } - delete stat; + deleteERStatTree(stat); } er_stack.clear();