aboutsummaryrefslogtreecommitdiff
path: root/src/loader.c
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2016-09-28 00:14:45 +0100
committerHarry Jeffery <harry@exec64.co.uk>2016-09-28 00:14:45 +0100
commite4ded04c39a2ceca64d390b7aa998e7537cf6a2c (patch)
tree09307538adac1862185c40aa06420eba19872aaa /src/loader.c
parent10e71e07e4b2bbd451b35a493431928d44054185 (diff)
downloadimv-e4ded04c39a2ceca64d390b7aa998e7537cf6a2c.tar.gz
Fix thread resource leak in loader
Diffstat (limited to 'src/loader.c')
-rw-r--r--src/loader.c10
1 files changed, 9 insertions, 1 deletions
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);
}