aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2020-10-06 17:16:39 +0200
committerChristian Tismer <tismer@stackless.com>2020-10-06 23:02:25 +0000
commita0f3062ebb0775e6072592991c18fa6bc1909497 (patch)
tree403394a93d9be3f8fc2568a661d09998a816d31f
parentc8e6baea6015cb687fc049a90caa3866476bbe5e (diff)
Fix leak in static string destruction, addendum
When checking a debug build, refcounting problems showed up. The reason was that somewhere the static strings were treated with normal refcounting. This is easily possible if some string is used in the interpreter: The string becomes interned, maybe before it is used by us, and will be deallocated without our knowledge. We are turning the list of referenced strings into a set. Also, the strings are made immortal by adding one extra count. This way, our set does not need to cope with deletions and will work either way. The final cleanup removes that, anyway. Change-Id: I192ea47f739cc785ea3e354cfb82c5fa5a6eac78 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
-rw-r--r--sources/shiboken2/libshiboken/sbkstring.cpp11
1 files changed, 8 insertions, 3 deletions
diff --git a/sources/shiboken2/libshiboken/sbkstring.cpp b/sources/shiboken2/libshiboken/sbkstring.cpp
index 1fb176a4c..918aae756 100644
--- a/sources/shiboken2/libshiboken/sbkstring.cpp
+++ b/sources/shiboken2/libshiboken/sbkstring.cpp
@@ -271,10 +271,15 @@ PyObject *createStaticString(const char *str)
Py_FatalError("unexpected error in createStaticString()");
}
auto it = staticStrings().find(result);
- if (it != staticStrings().end())
- Py_INCREF(result);
- else
+ if (it == staticStrings().end())
staticStrings().insert(result);
+ /*
+ * Note: We always add one reference even if we have a new string.
+ * This makes the strings immortal, and we are safe if someone
+ * uses AutoDecRef, although the set cannot cope with deletions.
+ * The exit handler cleans that up, anyway.
+ */
+ Py_INCREF(result);
return result;
}