thread_leak_test.c 1.5 KB

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