Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using SURF with TBB support: thread leak?


I am using Ubuntu 11.04 with OpenCV 2.4.2, compiled with TBB support (TBB version 3.0) with an Intel i5 processor (4 cores). I have tried using the parallelized version of SURF descriptors extraction. Either there is something I did not understand well, or there might be a thread leak.

The idea is that I create a SURF extractor, use it on an image, then continue on in my code. With TBB, 4 threads are created for this extraction, but they are not destroyed even when the extraction is done, ven when the extractor object does not exist anymore.

Here is a piece of code to illustrate this

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <iostream>
#include <time.h>

int main(int argc, char* argv[]) {
  if (argc != 2) {
    return 1;
  cv::Mat img = cv::imread(argv[1], 0);
  // Scope where the extractor exists.
    cv::SURF extractor;
    std::vector<cv::KeyPoint> keypoints;
    std::vector<float> descriptors;
    std::cout << "Beginning extracting." << std::endl;
    int counter = 0;
    // Extract several times to see things happening
    while (counter < 10) {
      extractor(img, cv::Mat(), keypoints, descriptors);
      std::cout << "Iteration #" << counter << std::endl;
  // Now the extractor does not exist anymore
  std::cout << "Finished extracting." << std::endl;
  // Wait a bit to check the threads.
  std::cout << "Exiting" << std::endl;
  return 0;


If you run the program (giving an image path as an argument) and check the number of threads used, you can see that the for threads are created, but stay until the end of the program, even when the extracting is done and the extractor object does not exist. This looks to me like a thread leak...

So, do I misuse SURF extraction, is this a normal behavior, or a bug ?

Thanks in advance