diff options
author | Campbell Barton <campbell@blender.org> | 2022-07-07 05:30:45 +0300 |
---|---|---|
committer | Campbell Barton <campbell@blender.org> | 2022-07-07 05:30:45 +0300 |
commit | 83c0f6ac3740cc8d7fb17a8b837ac95a8956805c (patch) | |
tree | cf069a3afc487a3067eb53e31698fe1e0df04f87 | |
parent | 5c790fd52b6b5c9ebd28c8eaac49a2ba6d7e9a7a (diff) |
Python: clear Py-driver variables on exit
These kinds of leaks are relatively harmless, it reduces the number of
un-freed data reported by valgrind on exit.
-rw-r--r-- | source/blender/python/BPY_extern.h | 5 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_driver.c | 22 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 3 |
3 files changed, 22 insertions, 8 deletions
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index 0ab26fde34f..8075e4ecd22 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -70,6 +70,11 @@ void BPY_modules_load_user(struct bContext *C); void BPY_app_handlers_reset(bool do_all); /** + * Run on exit to free any cached data. + */ +void BPY_driver_exit(void); + +/** * Update function, it gets rid of python-drivers global dictionary: `bpy.app.driver_namespace`, * forcing #BPY_driver_exec to recreate it. Use this when loading a new `.blend` file * so any variables setup by the previous blend file are cleared. diff --git a/source/blender/python/intern/bpy_driver.c b/source/blender/python/intern/bpy_driver.c index aa627a42a2b..3134969d21c 100644 --- a/source/blender/python/intern/bpy_driver.c +++ b/source/blender/python/intern/bpy_driver.c @@ -233,15 +233,8 @@ static void bpy_pydriver_namespace_update_depsgraph(struct Depsgraph *depsgraph) } } -void BPY_driver_reset(void) +void BPY_driver_exit(void) { - PyGILState_STATE gilstate; - const bool use_gil = true; /* !PyC_IsInterpreterActive(); */ - - if (use_gil) { - gilstate = PyGILState_Ensure(); - } - if (bpy_pydriver_Dict) { /* Free the global dict used by python-drivers. */ PyDict_Clear(bpy_pydriver_Dict); Py_DECREF(bpy_pydriver_Dict); @@ -261,6 +254,19 @@ void BPY_driver_reset(void) /* Freed when clearing driver dictionary. */ g_pydriver_state_prev.self = NULL; g_pydriver_state_prev.depsgraph = NULL; +} + +void BPY_driver_reset(void) +{ + PyGILState_STATE gilstate; + const bool use_gil = true; /* !PyC_IsInterpreterActive(); */ + + if (use_gil) { + gilstate = PyGILState_Ensure(); + } + + /* Currently exit/reset are practically the same besides the GIL check. */ + BPY_driver_exit(); if (use_gil) { PyGILState_Release(gilstate); diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index ea64fa6c098..939fa475344 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -512,6 +512,9 @@ void BPY_python_end(void) /* finalizing, no need to grab the state, except when we are a module */ gilstate = PyGILState_Ensure(); + /* Frees the python-driver name-space & cached data. */ + BPY_driver_exit(); + /* Clear Python values in the context so freeing the context after Python exits doesn't crash. */ bpy_context_end(BPY_context_get()); |