Skip to content
This repository
Browse code

[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
authored October 02, 2012
26  xbmc/interfaces/python/swig.cpp
@@ -33,8 +33,18 @@ namespace PythonBindings
33 33
     memset(typeInfo, 0, sizeof(TypeInfo));
34 34
   }
35 35
 
36  
-  int PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString,
37  
-                             const char* argumentName, const char* methodname) throw (XBMCAddon::WrongTypeException)
  36
+  class PyObjectDecrementor
  37
+  {
  38
+    PyObject* obj;
  39
+  public:
  40
+    inline PyObjectDecrementor(PyObject* pyobj) : obj(pyobj) {}
  41
+    inline ~PyObjectDecrementor() { Py_XDECREF(obj); }
  42
+
  43
+    inline PyObject* get() { return obj; }
  44
+  };
  45
+
  46
+  void PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString,
  47
+                              const char* argumentName, const char* methodname) throw (XBMCAddon::WrongTypeException)
38 48
   {
39 49
     // TODO: UTF-8: Does python use UTF-16?
40 50
     //              Do we need to convert from the string charset to UTF-8
@@ -50,24 +60,24 @@ namespace PythonBindings
50 60
       {
51 61
         buf = PyString_AsString(utf8_pyString);
52 62
         Py_DECREF(utf8_pyString);
53  
-        return 1;
  63
+        return;
54 64
       }
55 65
     }
56 66
     if (PyString_Check(pObject))
57 67
     {
58 68
       buf = PyString_AsString(pObject);
59  
-      return 1;
  69
+      return;
60 70
     }
61 71
 
62 72
     // if we got here then we need to coerce the value to a string
63 73
     if (coerceToString)
64 74
     {
65  
-      PyObject* pyStrCast = PyObject_Str(pObject);
  75
+      PyObjectDecrementor dec(PyObject_Str(pObject));
  76
+      PyObject* pyStrCast = dec.get();
66 77
       if (pyStrCast)
67 78
       {
68  
-        int ret = PyXBMCGetUnicodeString(buf,pyStrCast,false,argumentName,methodname);
69  
-        Py_DECREF(pyStrCast);
70  
-        return ret;
  79
+        PyXBMCGetUnicodeString(buf,pyStrCast,false,argumentName,methodname);
  80
+        return;
71 81
       }
72 82
     }
73 83
 
6  xbmc/interfaces/python/swig.h
@@ -30,9 +30,9 @@
30 30
 
31 31
 namespace PythonBindings
32 32
 {
33  
-  int PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString = false,
34  
-                             const char* pos = "unknown", 
35  
-                             const char* methodname = "unknown") throw (XBMCAddon::WrongTypeException);
  33
+  void PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString = false,
  34
+                              const char* pos = "unknown", 
  35
+                              const char* methodname = "unknown") throw (XBMCAddon::WrongTypeException);
36 36
 
37 37
   // This is for casting from child class to base class
38 38
   struct TypeConverterBase

0 notes on commit d925641

Please sign in to comment.
Something went wrong with that request. Please try again.