thread_leak_test.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifdef HAVE_CONFIG_H
  2. # include "config.h"
  3. #endif
  4. #ifndef GC_THREADS
  5. # define GC_THREADS
  6. #endif
  7. #undef GC_NO_THREAD_REDIRECTS
  8. #include "leak_detector.h"
  9. #ifdef GC_PTHREADS
  10. # include <pthread.h>
  11. #else
  12. # include <windows.h>
  13. #endif
  14. #include <stdio.h>
  15. #ifdef GC_PTHREADS
  16. void * test(void * arg)
  17. #else
  18. DWORD WINAPI test(LPVOID arg)
  19. #endif
  20. {
  21. int *p[10];
  22. int i;
  23. for (i = 0; i < 10; ++i) {
  24. p[i] = (int *)malloc(sizeof(int) + i);
  25. }
  26. CHECK_LEAKS();
  27. for (i = 1; i < 10; ++i) {
  28. free(p[i]);
  29. }
  30. # ifdef GC_PTHREADS
  31. return arg;
  32. # else
  33. return (DWORD)(GC_word)arg;
  34. # endif
  35. }
  36. #ifndef NTHREADS
  37. # define NTHREADS 5
  38. #endif
  39. int main(void) {
  40. # if NTHREADS > 0
  41. int i;
  42. # ifdef GC_PTHREADS
  43. pthread_t t[NTHREADS];
  44. # else
  45. HANDLE t[NTHREADS];
  46. DWORD thread_id;
  47. # endif
  48. int code;
  49. # endif
  50. GC_set_find_leak(1); /* for new collect versions not compiled */
  51. /* with -DFIND_LEAK. */
  52. GC_INIT();
  53. # if NTHREADS > 0
  54. for (i = 0; i < NTHREADS; ++i) {
  55. # ifdef GC_PTHREADS
  56. code = pthread_create(t + i, 0, test, 0);
  57. # else
  58. t[i] = CreateThread(NULL, 0, test, 0, 0, &thread_id);
  59. code = t[i] != NULL ? 0 : (int)GetLastError();
  60. # endif
  61. if (code != 0) {
  62. fprintf(stderr, "Thread creation failed %d\n", code);
  63. exit(2);
  64. }
  65. }
  66. for (i = 0; i < NTHREADS; ++i) {
  67. # ifdef GC_PTHREADS
  68. code = pthread_join(t[i], 0);
  69. # else
  70. code = WaitForSingleObject(t[i], INFINITE) == WAIT_OBJECT_0 ? 0 :
  71. (int)GetLastError();
  72. # endif
  73. if (code != 0) {
  74. fprintf(stderr, "Thread join failed %d\n", code);
  75. exit(2);
  76. }
  77. }
  78. # endif
  79. CHECK_LEAKS();
  80. CHECK_LEAKS();
  81. CHECK_LEAKS();
  82. return 0;
  83. }