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.
This commit is contained in:
Kévin Dietrich 2016-08-26 14:25:03 +02:00
parent 8870c454a1
commit 62b1cdd66e
5 changed files with 27 additions and 0 deletions

@ -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);

@ -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)

@ -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);

@ -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. */

@ -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();