aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/libshiboken/sbkstring.cpp
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2020-10-06 11:55:32 +0200
committerChristian Tismer <tismer@stackless.com>2020-10-06 15:32:45 +0200
commite23d2a54b02dc31f980bfeaff463706c7fef782a (patch)
treefdb15ea7bf0c24fa49d61c1d9119541ab70ed652 /sources/shiboken2/libshiboken/sbkstring.cpp
parent80f8731ee894d3cd40be9a2a0f3778f60dbc6afa (diff)
Fix leak in static string destruction
When checking a debug build, refcounting problems showed up. It is unclear where references are lost, at the moment at the strings "__div__" and "__idiv__". It is possible that these strings were already in a destruction list of another module. When turning the list into a set, the problem vanishes, because we no longer know how much references to expect. Because they are not negative, this seems to be ok. Change-Id: I0bc59b83f43becf5ee5f5f19213fc46513041909 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/shiboken2/libshiboken/sbkstring.cpp')
-rw-r--r--sources/shiboken2/libshiboken/sbkstring.cpp17
1 files changed, 12 insertions, 5 deletions
diff --git a/sources/shiboken2/libshiboken/sbkstring.cpp b/sources/shiboken2/libshiboken/sbkstring.cpp
index ba1cd1e47..1fb176a4c 100644
--- a/sources/shiboken2/libshiboken/sbkstring.cpp
+++ b/sources/shiboken2/libshiboken/sbkstring.cpp
@@ -42,6 +42,7 @@
#include "autodecref.h"
#include <vector>
+#include <unordered_set>
namespace Shiboken
{
@@ -232,7 +233,7 @@ Py_ssize_t len(PyObject *str)
// PyObject *attr = PyObject_GetAttr(obj, name());
//
-using StaticStrings = std::vector<PyObject *>;
+using StaticStrings = std::unordered_set<PyObject *>;
static void finalizeStaticStrings(); // forward
@@ -244,10 +245,12 @@ static StaticStrings &staticStrings()
static void finalizeStaticStrings()
{
- auto &list = staticStrings();
- for (PyObject *ob : list)
+ auto &set = staticStrings();
+ for (PyObject *ob : set) {
+ Py_REFCNT(ob) = 1;
Py_DECREF(ob);
- list.clear();
+ }
+ set.clear();
}
PyObject *createStaticString(const char *str)
@@ -267,7 +270,11 @@ PyObject *createStaticString(const char *str)
PyErr_Print();
Py_FatalError("unexpected error in createStaticString()");
}
- staticStrings().push_back(result);
+ auto it = staticStrings().find(result);
+ if (it != staticStrings().end())
+ Py_INCREF(result);
+ else
+ staticStrings().insert(result);
return result;
}