Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[fix] python object memory leak when we fail on an attempt to coerce …

…a python unicode to a string.
  • Loading branch information...
commit d925641b5033296dbdc4046328887f84885f71ac 1 parent 6cf5b9d
Jim Carroll authored
Showing with 21 additions and 11 deletions.
  1. +18 −8 xbmc/interfaces/python/swig.cpp
  2. +3 −3 xbmc/interfaces/python/swig.h
View
26 xbmc/interfaces/python/swig.cpp
@@ -33,8 +33,18 @@ namespace PythonBindings
memset(typeInfo, 0, sizeof(TypeInfo));
}
- int PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString,
- const char* argumentName, const char* methodname) throw (XBMCAddon::WrongTypeException)
+ class PyObjectDecrementor
+ {
+ PyObject* obj;
+ public:
+ inline PyObjectDecrementor(PyObject* pyobj) : obj(pyobj) {}
+ inline ~PyObjectDecrementor() { Py_XDECREF(obj); }
+
+ inline PyObject* get() { return obj; }
+ };
+
+ void PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString,
+ const char* argumentName, const char* methodname) throw (XBMCAddon::WrongTypeException)
{
// TODO: UTF-8: Does python use UTF-16?
// Do we need to convert from the string charset to UTF-8
@@ -50,24 +60,24 @@ namespace PythonBindings
{
buf = PyString_AsString(utf8_pyString);
Py_DECREF(utf8_pyString);
- return 1;
+ return;
}
}
if (PyString_Check(pObject))
{
buf = PyString_AsString(pObject);
- return 1;
+ return;
}
// if we got here then we need to coerce the value to a string
if (coerceToString)
{
- PyObject* pyStrCast = PyObject_Str(pObject);
+ PyObjectDecrementor dec(PyObject_Str(pObject));
+ PyObject* pyStrCast = dec.get();
if (pyStrCast)
{
- int ret = PyXBMCGetUnicodeString(buf,pyStrCast,false,argumentName,methodname);
- Py_DECREF(pyStrCast);
- return ret;
+ PyXBMCGetUnicodeString(buf,pyStrCast,false,argumentName,methodname);
+ return;
}
}
View
6 xbmc/interfaces/python/swig.h
@@ -30,9 +30,9 @@
namespace PythonBindings
{
- int PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString = false,
- const char* pos = "unknown",
- const char* methodname = "unknown") throw (XBMCAddon::WrongTypeException);
+ void PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString = false,
+ const char* pos = "unknown",
+ const char* methodname = "unknown") throw (XBMCAddon::WrongTypeException);
// This is for casting from child class to base class
struct TypeConverterBase
Please sign in to comment.
Something went wrong with that request. Please try again.