Need help with memory leak while multi-threading

There is a memory leak below when the struct is being passed to a thread. Can not understand why, as the code inside the thread if called directly in the main thread does not leak memory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class PeopleCounting{

  // Class variables
  Ptr<cv::BackgroundSubtractorMOG2> pMOG2 = cv::createBackgroundSubtractorMOG2(500, 16);
  Mat maskBackgroundSubtracted = Mat(resizeDimension.height, resizeDimension.width, CV_8UC1);

  // Thread creation code below, code called from main.

    //Create thread
    pthread_t threads;
    pthread_attr_t attr;
    void *status;

    // Initialize and set thread joinable
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    // Creating thread data and initializing it
    BackgroundSubstractionThreadData threadData = {CamImage, maskBackgroundSubtracted, pMOG2};
    int rc;
    rc = pthread_create(&threads, NULL, performBackgroundSubstraction, (void *)&threadData);
    if (rc)
    {
        __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error: peopleCountingMainMono unable to create thread  - %d",rc);
    }
    // free attribute and wait for the other threads
    pthread_attr_destroy(&attr);

    // ************** Do something else in main thread **************

    // Join thread i.e. wait till completion of thread
    rc = pthread_join(threads, &status);
    if (rc)
    {
        __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error: peopleCountingMainMono unable to join - %d",rc);
    }

    // Using class variable **maskBackgroundSubtracted** and **pMOG2** for later use. **CamImage** (opencv mat) usually gets released automatically in general due to smart pointer implementation, not sure if it is the source of leak


}

// Note: Outside class
void *performBackgroundSubstraction(void *threadarg)
  {
  struct BackgroundSubstractionThreadData *my_data;
  my_data = (struct BackgroundSubstractionThreadData *)threadarg;

  Mat fgMask;
  my_data->pMOG2F->apply(my_data->leftCamImage, fgMask, 0.002);

  morphologyEx(fgMask, fgMask, MORPH_OPEN, getStructuringElement(MORPH_RECT, Size(3, 3)),Point(-1,-1),1);
  morphologyEx(fgMask, fgMask, MORPH_CLOSE, getStructuringElement(MORPH_RECT, Size(11, 11)),Point(-1,-1),1);
  threshold(fgMask, my_data->dst, 128, 255, THRESH_BINARY);
  pthread_exit(NULL);
  };
Last edited on
I doubt that's enough code. You should provide a link to a zip file of the whole shebang.

In future, use [code] [/code] tags when posting code. You can edit the above and repaste the code.
Last edited on
There is a memory leak below when the struct is being passed to a thread.
No there isn't.

My main concern is cv::BackgroundSubtractorMOG2::apply() as it's called asynchronously.

BTW, you do realize that:
 
Mat maskBackgroundSubtracted = Mat(resizeDimension.height, resizeDimension.width, CV_8UC1)


could be just:
 
Mat maskBackgroundSubtracted(resizeDimension.height, resizeDimension.width, CV_8UC1);


right?
For any one coming here, pthread_exit(NULL) was the culprit. Changed it to return 0 and it solved the issue.
It sounds like pthread_exit() didn't call a destructor. Since you're using C++, maybe you should use C++ threads instead of posix threads: http://www.cplusplus.com/reference/thread/.
Topic archived. No new replies allowed.