Fix crash when freeing Blender after GTests
authorSybren A. Stüvel <sybren@blender.org>
Fri, 15 Nov 2019 14:13:06 +0000 (15:13 +0100)
committerSybren A. Stüvel <sybren@blender.org>
Fri, 15 Nov 2019 14:13:18 +0000 (15:13 +0100)
This only frees brush_rng and random_tex_array when they were actually
previously allocated.

In a unit test (see D6246) I want to be able to partially start Blender
so that I can load a blend file. To prevent memory leaks, I also want to
be able to release memory, which currently requires calling
`BKE_blender_free()`. This unconditionally calls `RE_texture_rng_exit()`
and `BKE_brush_system_exit()`, which now crash on freeing `NULL`. This
patch fixes that.

Allocation (`BKE_brush_system_init()`) and freeing
(`BKE_brush_system_exit()`) are done asymmetrically. The allocation
functions are called from `main()` in the creator module, but the
freeing is done by `BKE_blender_free()` the Window Manager. Ideally we
symmetrise this and initialise Blender from outside the window manager
(so that the initialisation can be done without WM and Python too), but
for now I'm happy when things don't crash.

Reviewed by: sergey via pair programming

source/blender/blenkernel/intern/brush.c
source/blender/render/intern/source/render_texture.c

index bb9db8d9b9051e28346fbcf274d88241273790db..75669e8289a7c76374a1a9202457a8f0a85dc306 100644 (file)
@@ -58,7 +58,11 @@ void BKE_brush_system_init(void)
 
 void BKE_brush_system_exit(void)
 {
+  if (brush_rng == NULL) {
+    return;
+  }
   BLI_rng_free(brush_rng);
+  brush_rng = NULL;
 }
 
 static void brush_defaults(Brush *brush)
index be53a1cb067c7857f5aed048c58056b606c79d15..3f3dc9842a59fcad6d951c2b04b9e1274ce6034a 100644 (file)
@@ -70,7 +70,11 @@ void RE_texture_rng_init(void)
 
 void RE_texture_rng_exit(void)
 {
+  if (random_tex_array == NULL) {
+    return;
+  }
   BLI_rng_threaded_free(random_tex_array);
+  random_tex_array = NULL;
 }
 
 /* ------------------------------------------------------------------------- */