[Bf-blender-cvs] [62b1cdd] master: Fix over creation of cache files handles (leading to memory leaks).

Kévin Dietrich noreply at git.blender.org
Fri Aug 26 14:29:02 CEST 2016


Commit: 62b1cdd66e39db58e422e15b20a80d5e05f3dd4f
Author: Kévin Dietrich
Date:   Fri Aug 26 14:25:03 2016 +0200
Branches: master
https://developer.blender.org/rB62b1cdd66e39db58e422e15b20a80d5e05f3dd4f

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.

===================================================================

M	source/blender/blenkernel/BKE_cachefile.h
M	source/blender/blenkernel/intern/cachefile.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/makesdna/DNA_cachefile_types.h
M	source/creator/creator.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_cachefile.h b/source/blender/blenkernel/BKE_cachefile.h
index 51b161f..7a9744e 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 16f2637..502f1d5 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 7d65dbc..f5bfe1f 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 1cda023..dd47d63 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 bfb1dd9..a59a45f 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();




More information about the Bf-blender-cvs mailing list