TIVI-153: Add as dependency for Iputils
[profile/ivi/gc.git] / tests / thread_leak_test.c
1 #ifndef GC_THREADS
2 #  define GC_THREADS
3 #endif /* GC_THREADS */
4 #include "leak_detector.h"
5 #include <pthread.h>
6 #include <stdio.h>
7
8 void * test(void * arg) {
9     int *p[10];
10     int i;
11     GC_find_leak = 1; /* for new collect versions not compiled  */
12     /* with -DFIND_LEAK.                                        */
13     for (i = 0; i < 10; ++i) {
14         p[i] = malloc(sizeof(int)+i);
15     }
16     CHECK_LEAKS();
17     for (i = 1; i < 10; ++i) {
18         free(p[i]);
19     }
20     return 0;
21 }       
22
23 #define NTHREADS 5
24
25 main() {
26     int i;
27     pthread_t t[NTHREADS];
28     int code;
29
30     GC_INIT();
31     for (i = 0; i < NTHREADS; ++i) {
32         if ((code = pthread_create(t + i, 0, test, 0)) != 0) {
33             printf("Thread creation failed %d\n", code);
34         }
35     }
36     for (i = 0; i < NTHREADS; ++i) {
37         if ((code = pthread_join(t[i], 0)) != 0) {
38             printf("Thread join failed %lu\n", code);
39         }
40     }
41     CHECK_LEAKS();
42     CHECK_LEAKS();
43     CHECK_LEAKS();
44     return 0;
45 }