From cbc671947a3baca3da7d6c5a2980a86b7f6a7055 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 11 Aug 2021 16:56:11 +1000 Subject: Fix T88033: Python reference memory leaks for non main data-blocks ID data-blocks that could be accessed from Python and weren't freed using BKE_id_free_ex did not release the Python reference count. Add BKE_libblock_free_data_py function to clear the Python reference in this case. Add asserts to ensure no Python reference is held in situations when ID's are copied for internal use (not exposed through the RNA API), to ensure these kinds of leaks don't go by unnoticed again. --- source/blender/blenkernel/intern/lib_id_delete.c | 35 ++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'source/blender/blenkernel/intern/lib_id_delete.c') diff --git a/source/blender/blenkernel/intern/lib_id_delete.c b/source/blender/blenkernel/intern/lib_id_delete.c index a9407860c06..43afac5a376 100644 --- a/source/blender/blenkernel/intern/lib_id_delete.c +++ b/source/blender/blenkernel/intern/lib_id_delete.c @@ -142,14 +142,7 @@ void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_i DEG_id_type_tag(bmain, type); } -#ifdef WITH_PYTHON -# ifdef WITH_PYTHON_SAFETY - BPY_id_release(id); -# endif - if (id->py_instance) { - BPY_DECREF_RNA_INVALIDATE(id->py_instance); - } -#endif + BKE_libblock_free_data_py(id); Key *key = ((flag & LIB_ID_FREE_NO_MAIN) == 0) ? BKE_key_from_id(id) : NULL; @@ -406,3 +399,29 @@ size_t BKE_id_multi_tagged_delete(Main *bmain) { return id_delete(bmain, true); } + +/* -------------------------------------------------------------------- */ +/** \name Python Data Handling + * \{ */ + +/** + * In most cases #BKE_id_free_ex handles this, when lower level functions are called directly + * this function will need to be called too, if Python has access to the data. + * + * ID data-blocks such as #Material.nodetree are not stored in #Main. + */ +void BKE_libblock_free_data_py(ID *id) +{ +#ifdef WITH_PYTHON +# ifdef WITH_PYTHON_SAFETY + BPY_id_release(id); +# endif + if (id->py_instance) { + BPY_DECREF_RNA_INVALIDATE(id->py_instance); + } +#else + UNUSED_VARS(id); +#endif +} + +/** \} */ -- cgit v1.2.3