From 62b1cdd66e39db58e422e15b20a80d5e05f3dd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dietrich?= Date: Fri, 26 Aug 2016 14:25:03 +0200 Subject: Fix over creation of cache files handles (leading to memory leaks). Multiple threads could create multiple handles for the same cache file, so protect handle creation with a mutex, to make sure only one is created. --- source/blender/blenkernel/BKE_cachefile.h | 2 ++ source/blender/blenkernel/intern/cachefile.c | 20 ++++++++++++++++++++ source/blender/blenloader/intern/readfile.c | 2 ++ source/blender/makesdna/DNA_cachefile_types.h | 1 + source/creator/creator.c | 2 ++ 5 files changed, 27 insertions(+) diff --git a/source/blender/blenkernel/BKE_cachefile.h b/source/blender/blenkernel/BKE_cachefile.h index 51b161f1a06..7a9744ef9d6 100644 --- a/source/blender/blenkernel/BKE_cachefile.h +++ b/source/blender/blenkernel/BKE_cachefile.h @@ -38,6 +38,8 @@ struct CacheFile; struct Main; struct Scene; +void BKE_cachefiles_init(void); + void *BKE_cachefile_add(struct Main *bmain, const char *name); void BKE_cachefile_init(struct CacheFile *cache_file); diff --git a/source/blender/blenkernel/intern/cachefile.c b/source/blender/blenkernel/intern/cachefile.c index 16f263791db..502f1d53ab2 100644 --- a/source/blender/blenkernel/intern/cachefile.c +++ b/source/blender/blenkernel/intern/cachefile.c @@ -35,6 +35,7 @@ #include "BLI_listbase.h" #include "BLI_path_util.h" #include "BLI_string.h" +#include "BLI_threads.h" #include "BLI_utildefines.h" #include "BKE_animsys.h" @@ -48,6 +49,13 @@ # include "ABC_alembic.h" #endif +static SpinLock spin; + +void BKE_cachefiles_init(void) +{ + BLI_spin_init(&spin); +} + void *BKE_cachefile_add(Main *bmain, const char *name) { CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, name); @@ -65,6 +73,7 @@ void BKE_cachefile_init(CacheFile *cache_file) cache_file->frame = 0.0f; cache_file->is_sequence = false; cache_file->scale = 1.0f; + cache_file->handle_mutex = BLI_mutex_alloc(); } /** Free (or release) any data used by this cachefile (does not free the cachefile itself). */ @@ -76,6 +85,7 @@ void BKE_cachefile_free(CacheFile *cache_file) ABC_free_handle(cache_file->handle); #endif + BLI_mutex_free(cache_file->handle_mutex); BLI_freelistN(&cache_file->object_paths); } @@ -114,9 +124,19 @@ void BKE_cachefile_reload(const Main *bmain, CacheFile *cache_file) void BKE_cachefile_ensure_handle(const Main *bmain, CacheFile *cache_file) { + BLI_spin_lock(&spin); + if (cache_file->handle_mutex == NULL) { + cache_file->handle_mutex = BLI_mutex_alloc(); + } + BLI_spin_unlock(&spin); + + BLI_mutex_lock(cache_file->handle_mutex); + if (cache_file->handle == NULL) { BKE_cachefile_reload(bmain, cache_file); } + + BLI_mutex_unlock(cache_file->handle_mutex); } void BKE_cachefile_update_frame(Main *bmain, Scene *scene, const float ctime, const float fps) diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index 7d65dbc2e17..f5bfe1f4de6 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -2707,6 +2707,7 @@ static void lib_link_cachefiles(FileData *fd, Main *bmain) BLI_listbase_clear(&cache_file->object_paths); cache_file->handle = NULL; + cache_file->handle_mutex = NULL; if (cache_file->adt) { lib_link_animdata(fd, &cache_file->id, cache_file->adt); @@ -2717,6 +2718,7 @@ static void lib_link_cachefiles(FileData *fd, Main *bmain) static void direct_link_cachefile(FileData *fd, CacheFile *cache_file) { cache_file->handle = NULL; + cache_file->handle_mutex = NULL; /* relink animdata */ cache_file->adt = newdataadr(fd, cache_file->adt); diff --git a/source/blender/makesdna/DNA_cachefile_types.h b/source/blender/makesdna/DNA_cachefile_types.h index 1cda0233aa8..dd47d63fc19 100644 --- a/source/blender/makesdna/DNA_cachefile_types.h +++ b/source/blender/makesdna/DNA_cachefile_types.h @@ -58,6 +58,7 @@ typedef struct CacheFile { struct AnimData *adt; struct AbcArchiveHandle *handle; + void *handle_mutex; /* Paths of the objects inside of the Alembic archive referenced by this * CacheFile. */ diff --git a/source/creator/creator.c b/source/creator/creator.c index bfb1dd94dd4..a59a45f885c 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -54,6 +54,7 @@ #include "BKE_appdir.h" #include "BKE_blender.h" #include "BKE_brush.h" +#include "BKE_cachefile.h" #include "BKE_context.h" #include "BKE_depsgraph.h" /* for DAG_init */ #include "BKE_font.h" @@ -357,6 +358,7 @@ int main( BKE_blender_globals_init(); /* blender.c */ IMB_init(); + BKE_cachefiles_init(); BKE_images_init(); BKE_modifier_init(); DAG_init(); -- cgit v1.2.3