Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Fix thread handle leak on win32
Browse files Browse the repository at this point in the history
  • Loading branch information
thenickdude committed Mar 26, 2015
1 parent b755bb6 commit 602b382
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/blackbox_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,6 @@ void saveSurfaceAsync(cairo_surface_t *surface, int logIndex, int outputFrameInd
pngRenderingSemCreated = true;
}

thread_t thread;
pngRenderingTask_t *task = (pngRenderingTask_t*) malloc(sizeof(*task));

task->surface = surface;
Expand All @@ -994,7 +993,7 @@ void saveSurfaceAsync(cairo_surface_t *surface, int logIndex, int outputFrameInd
// Reserve a slot in the rendering pool...
semaphore_wait(&pngRenderingSem);

thread_create(&thread, pngRenderThread, task);
thread_create_detached(pngRenderThread, task);
}

void waitForFramesToSave()
Expand Down
11 changes: 8 additions & 3 deletions src/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,22 @@
}
#endif

void thread_create(thread_t *thread, threadRoutine_t threadFunc, void *data)
void thread_create_detached(threadRoutine_t threadFunc, void *data)
{
thread_t thread;

#if defined(WIN32)
win32ThreadFuncWrapper_t *wrap = malloc(sizeof(*wrap));

wrap->threadFunc = threadFunc;
wrap->data = data;

*thread = CreateThread(NULL, 0, win32ThreadFuncUnwrap, wrap, 0, NULL);
thread = CreateThread(NULL, 0, win32ThreadFuncUnwrap, wrap, 0, NULL);

// Detach from thread immediately
CloseHandle(thread);
#else
pthread_create(thread, &pthreadCreateDetached, threadFunc, data);
pthread_create(&thread, &pthreadCreateDetached, threadFunc, data);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ typedef struct fileMapping_t {

typedef void*(*threadRoutine_t)(void *data);

void thread_create(thread_t *thread, threadRoutine_t threadFunc, void *data);
void thread_create_detached(threadRoutine_t threadFunc, void *data);

bool mmap_file(fileMapping_t *mapping, int fd);
void munmap_file(fileMapping_t *mapping);
Expand Down

0 comments on commit 602b382

Please sign in to comment.