PyAPI: fix leak linking library data
authorCampbell Barton <ideasman42@gmail.com>
Thu, 14 Jul 2016 07:38:22 +0000 (17:38 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 14 Jul 2016 07:38:22 +0000 (17:38 +1000)
source/blender/python/intern/bpy_library_load.c

index 9c417cfd221a55363d57b53e976668a128c673d9..7ceb99d5add6bcf0565875c353b56d12d6d2eef6 100644 (file)
@@ -351,30 +351,30 @@ static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
                                        /* loop */
                                        Py_ssize_t size = PyList_GET_SIZE(ls);
                                        Py_ssize_t i;
-                                       PyObject *item;
-                                       const char *item_str;
 
                                        for (i = 0; i < size; i++) {
-                                               item = PyList_GET_ITEM(ls, i);
-                                               item_str = _PyUnicode_AsString(item);
+                                               PyObject *item_src = PyList_GET_ITEM(ls, i);
+#ifdef USE_RNA_DATABLOCKS
+                                               PyObject *item_dst = NULL;
+#endif
+                                               const char *item_idname = _PyUnicode_AsString(item_src);
 
-                                               // printf("  %s\n", item_str);
+                                               // printf("  %s\n", item_idname);
 
-                                               if (item_str) {
-                                                       ID *id = BLO_library_link_named_part(mainl, &(self->blo_handle), idcode, item_str);
+                                               if (item_idname) {
+                                                       ID *id = BLO_library_link_named_part(mainl, &(self->blo_handle), idcode, item_idname);
                                                        if (id) {
 #ifdef USE_RNA_DATABLOCKS
                                                                /* swap name for pointer to the id */
-                                                               Py_DECREF(item);
-                                                               item = PyCapsule_New((void *)id, NULL, NULL);
+                                                               item_dst = PyCapsule_New((void *)id, NULL, NULL);
 #endif
                                                        }
                                                        else {
-                                                               bpy_lib_exit_warn_idname(self, name_plural, item_str);
+                                                               bpy_lib_exit_warn_idname(self, name_plural, item_idname);
                                                                /* just warn for now */
                                                                /* err = -1; */
 #ifdef USE_RNA_DATABLOCKS
-                                                               item = Py_INCREF_RET(Py_None);
+                                                               item_dst = Py_INCREF_RET(Py_None);
 #endif
                                                        }
 
@@ -382,16 +382,20 @@ static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
                                                }
                                                else {
                                                        /* XXX, could complain about this */
-                                                       bpy_lib_exit_warn_type(self, item);
+                                                       bpy_lib_exit_warn_type(self, item_src);
                                                        PyErr_Clear();
 
 #ifdef USE_RNA_DATABLOCKS
-                                                       item = Py_INCREF_RET(Py_None);
+                                                       item_dst = Py_INCREF_RET(Py_None);
 #endif
                                                }
 
 #ifdef USE_RNA_DATABLOCKS
-                                               PyList_SET_ITEM(ls, i, item);
+                                               if (item_dst) {
+                                                       /* item_dst must be new or already incref'd */
+                                                       Py_DECREF(item_src);
+                                                       PyList_SET_ITEM(ls, i, item_dst);
+                                               }
 #endif
                                        }
                                }