From e4ded04c39a2ceca64d390b7aa998e7537cf6a2c Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Wed, 28 Sep 2016 00:14:45 +0100 Subject: Fix thread resource leak in loader --- src/loader.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/loader.c') diff --git a/src/loader.c b/src/loader.c index 0e7079e..72843a5 100644 --- a/src/loader.c +++ b/src/loader.c @@ -50,6 +50,13 @@ void imv_init_loader(struct imv_loader *ldr) pthread_mutex_init(&ldr->lock, NULL); /* ignore this signal in case we accidentally receive it */ block_usr1_signal(); + + /* create new threads with PTHREAD_CREATE_DETATCHED set, so that the threads + * are automatically cleaned up by the operating system, without an explicit + * call to pthread_join() + */ + pthread_attr_init(&ldr->thread_attrs); + pthread_attr_setdetachstate(&ldr->thread_attrs, PTHREAD_CREATE_DETACHED); } void imv_destroy_loader(struct imv_loader *ldr) @@ -57,6 +64,7 @@ void imv_destroy_loader(struct imv_loader *ldr) /* wait for any existing bg thread to finish */ pthread_join(ldr->bg_thread, NULL); pthread_mutex_destroy(&ldr->lock); + pthread_attr_destroy(&ldr->thread_attrs); if(ldr->bmp) { FreeImage_Unload(ldr->bmp); @@ -89,7 +97,7 @@ void imv_loader_load(struct imv_loader *ldr, const char *path, } else if (ldr->fi_buffer != NULL) { FreeImage_CloseMemory(ldr->fi_buffer); } - pthread_create(&ldr->bg_thread, NULL, &bg_new_img, ldr); + pthread_create(&ldr->bg_thread, &ldr->thread_attrs, &bg_new_img, ldr); pthread_mutex_unlock(&ldr->lock); } -- cgit v1.2.3