diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2016-09-28 00:14:45 +0100 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2016-09-28 00:14:45 +0100 |
commit | e4ded04c39a2ceca64d390b7aa998e7537cf6a2c (patch) | |
tree | 09307538adac1862185c40aa06420eba19872aaa /src | |
parent | 10e71e07e4b2bbd451b35a493431928d44054185 (diff) | |
download | imv-e4ded04c39a2ceca64d390b7aa998e7537cf6a2c.tar.gz |
Fix thread resource leak in loader
Diffstat (limited to 'src')
-rw-r--r-- | src/loader.c | 10 | ||||
-rw-r--r-- | src/loader.h | 1 |
2 files changed, 10 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); } diff --git a/src/loader.h b/src/loader.h index d2418a2..d8db6e8 100644 --- a/src/loader.h +++ b/src/loader.h @@ -42,6 +42,7 @@ struct imv_loader { int next_frame; int num_frames; double frame_time; + pthread_attr_t thread_attrs; }; /* Initialises an instance of imv_loader */ |