Memory leaks cv::kmeans in cv:grabCut

asked 2018-09-26 10:43:52 -0600

Jendker gravatar image

updated 2018-09-27 04:32:41 -0600

Hello everyone,

I am using cv::grabcut to remove the background from the face photos, but it seems, that it ruins the code. I am using OpenCV 3.4.2 and valgrind is showing me following issue:

<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: /home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests Orbik_3_combination</line>
</preamble>

<pid>23545</pid>
<ppid>765</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind.bin</exe>
    <arg>--tool=memcheck</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=/tmp/valgrind</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--leak-check=full</arg>
    <arg>--leak-resolution=med</arg>
    <arg>--track-origins=yes</arg>
  </vargv>
  <argv>
    <exe>/home/ben/Development/OpenCV_tests/cmake-build-release/OpenCV_tests</exe>
    <arg>Orbik_3_combination</arg>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.087 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:42:50.777 </time>
</status>

<error>
  <unique>0xa65</unique>
  <tid>1</tid>
  <kind>Leak_PossiblyLost</kind>
  <xwhat>
    <text>2,576 bytes in 7 blocks are possibly lost in loss record 2,662 of 2,701</text>
    <leakedbytes>2576</leakedbytes>
    <leakedblocks>7</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4C31B25</ip>
      <obj>/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>calloc</fn>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>allocate_dtv</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>286</line>
    </frame>
    <frame>
      <ip>0x40134A6</ip>
      <obj>/lib/x86_64-linux-gnu/ld-2.27.so</obj>
      <fn>_dl_allocate_tls</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/elf/../elf</dir>
      <file>dl-tls.c</file>
      <line>530</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>allocate_stack</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>allocatestack.c</file>
      <line>627</line>
    </frame>
    <frame>
      <ip>0xBD86227</ip>
      <obj>/lib/x86_64-linux-gnu/libpthread-2.27.so</obj>
      <fn>pthread_create@@GLIBC_2.2.5</fn>
      <dir>/build/glibc-OTsEL5/glibc-2.27/nptl</dir>
      <file>pthread_create.c</file>
      <line>644</line>
    </frame>
    <frame>
      <ip>0x8B2C044</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::WorkerThread::WorkerThread(cv::ThreadPool&amp;, unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2D851</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::ThreadPool::reconfigure_(unsigned int)</fn>
    </frame>
    <frame>
      <ip>0x8B2E7EC</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_pthreads(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x8B2A51C</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::parallel_for_(cv::Range const&amp;, cv::ParallelLoopBody const&amp;, double)</fn>
    </frame>
    <frame>
      <ip>0x89E09FA</ip>
      <obj>/usr/local/lib/libopencv_core.so.3.4.2</obj>
      <fn>cv::kmeans(cv::_InputArray const&amp;, int, cv::_InputOutputArray const&amp;, cv::TermCriteria, int, int, cv::_OutputArray const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x61FB3E4</ip>
      <obj>/usr/local/lib/libopencv_imgproc.so.3.4.2</obj>
      <fn>cv::grabCut(cv::_InputArray const&amp;, cv::_InputOutputArray const&amp;, cv::Rect_&lt;int&gt;, cv::_InputOutputArray const&amp;, cv::_InputOutputArray const&amp;, int, int)</fn>
    </frame> ...
(more)
edit retag flag offensive close merge delete

Comments

it's such a shame, your valgrind xml can't be properly formatted. (not really your fault).

berak gravatar imageberak ( 2018-09-26 11:12:49 -0600 )edit

What can I do to make it easier? I will try with the screenshot of CLion maybe, where I run valgrind...

Jendker gravatar imageJendker ( 2018-09-26 11:41:24 -0600 )edit

I was trying OpenCV master right now, but there are so many changes in the flags, that it is difficult to compile my program... Is it part of 4.0.0 alpha release?

Jendker gravatar imageJendker ( 2018-09-26 11:44:03 -0600 )edit

Is there any other algorithm in OpenCV which would allow me the image background segmentation?

imho, the most interesting (automatic) attempts at segmentation are here and here

berak gravatar imageberak ( 2018-09-26 12:00:16 -0600 )edit

Thanks! I will have a look on these. Here is screenshot from CLion, if there is any better way for a report please let me know... https://imgur.com/vycBqfO This was done with OpenCV release 3.4.3 (I updated it). valgrind call: /usr/bin/valgrind --tool=memcheck --xml=yes --xml-file=/tmp/valgrind --gen-suppressions=all --leak-check=full --leak-resolution=med --track-origins=yes <program name=""></program>

Jendker gravatar imageJendker ( 2018-09-26 12:08:17 -0600 )edit

however, i somehow suspect, your memleaks are a false alert. there are a few places in opencv, where they use static (singleton) buffers, which are needed for the whole lifetime of the program, and thus it's left to the os to clean it up. i think, you got one of those here (threadpool)

berak gravatar imageberak ( 2018-09-27 00:20:23 -0600 )edit

Thank you for your input! Unfortunately there is something wrong there, because if I run grabCut on the same data two times I get different results... Maybe there is something wrong with my implementation of segmentation with grabCut then? I get input and return output Mat https://paste.ofcode.org/PeiA7ppyurtC...

Jendker gravatar imageJendker ( 2018-09-27 03:44:53 -0600 )edit

please edit your question, and put your code there (not on an external bin, which will expire)

berak gravatar imageberak ( 2018-09-27 03:46:48 -0600 )edit
1

Post edited

Jendker gravatar imageJendker ( 2018-09-27 03:53:43 -0600 )edit

Hah ^^ I made the unit test of my grabCut function and by itself it works ok, but if I add haarCascade before it then I get different results for the same data when running in the loop... And if I unit test my haarCascade function it also runs ok... Just the combination seems to be problematic, gives different results. I am out of ideas here... I updated the post, but it is far from "minimal running example" here...

Jendker gravatar imageJendker ( 2018-09-27 04:26:37 -0600 )edit