Ask Your Question

emilie's profile - activity

2012-08-28 08:44:40 -0500 received badge  Student (source)
2012-08-16 07:37:23 -0500 commented answer Using SURF with TBB support: thread leak?

You're right. The thing is, by simply adding

cv::Ptr<tbb::task_scheduler_init> tbb_init = new tbb::task_scheduler_init();

right before the extractor initialization, the threads behave nicely and disappear when the tasks are done

2012-08-16 06:50:41 -0500 commented answer Using SURF with TBB support: thread leak?

Nice catch, had not seen that one... This looks exactly like my problem. I'll try to check if OpenCV uses auto initialization or not

2012-08-16 06:26:19 -0500 commented answer Using SURF with TBB support: thread leak?

Well, I'm not sure that this is normal once all tasks are done. If you look here in the "termination" section, it looks like worker threads are supposed to be removed once the tasks are done (at least, shortly afterwards).

2012-08-16 05:36:22 -0500 asked a question 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