fixed use of reference in pytalloc master4-talloc-old
authorAndrew Tridgell <tridge@samba.org>
Wed, 1 Jul 2009 04:05:17 +0000 (14:05 +1000)
committerAndrew Tridgell <tridge@samba.org>
Wed, 1 Jul 2009 05:15:37 +0000 (15:15 +1000)
The previous code caused memory leaks, and also caused situations
where talloc_free could be called on pointers with multiple parents

The new approach is to have two functions:

  py_talloc_import : steals the pointer, so it becomes wholly owned by
                     the python object
  py_talloc_reference: uses a reference, so it is owned by both python
                     and C

lib/talloc/pytalloc.c
lib/talloc/pytalloc.h

index 30da9ee5c2b964d5a07f9d1ecb793de0adde4cb5..3ce49d6d61fccb83d4ca93dbfcc995f911fc6695 100644 (file)
@@ -43,7 +43,27 @@ PyObject *py_talloc_import_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx,
        if (ret->talloc_ctx == NULL) {
                return NULL;
        }
-       if (talloc_reference(ret->talloc_ctx, mem_ctx) == NULL) {
+       if (talloc_steal(ret->talloc_ctx, mem_ctx) == NULL) {
+               return NULL;
+       }
+       ret->ptr = ptr;
+       return (PyObject *)ret;
+}
+
+
+/**
+ * Import an existing talloc pointer into a Python object, leaving the
+ * original parent, and creating a reference to the object in the python
+ * object
+ */
+PyObject *py_talloc_reference(PyTypeObject *py_type, void *ptr)
+{
+       py_talloc_Object *ret = (py_talloc_Object *)py_type->tp_alloc(py_type, 0);
+       ret->talloc_ctx = talloc_new(NULL);
+       if (ret->talloc_ctx == NULL) {
+               return NULL;
+       }
+       if (talloc_reference(ret->talloc_ctx, ptr) == NULL) {
                return NULL;
        }
        ret->ptr = ptr;
@@ -58,6 +78,6 @@ PyObject *py_talloc_default_repr(PyObject *obj)
        py_talloc_Object *talloc_obj = (py_talloc_Object *)obj;
        PyTypeObject *type = (PyTypeObject*)PyObject_Type(obj);
 
-       return PyString_FromFormat("<%s talloc object at 0x%x>", 
-                                  type->tp_name, (intptr_t)talloc_obj->ptr);
+       return PyString_FromFormat("<%s talloc object at 0x%p>", 
+                                  type->tp_name, talloc_obj->ptr);
 }
index c5a1428b29d9fca3681e06fef1b57be626b72b4a..00282c4ba0e972b0d76609a425922211a6a72157 100644 (file)
@@ -43,6 +43,7 @@ void py_talloc_dealloc(PyObject* self);
 #define py_talloc_get_mem_ctx(py_obj)  ((py_talloc_Object *)py_obj)->talloc_ctx
 
 PyObject *py_talloc_import_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr);
+PyObject *py_talloc_reference(PyTypeObject *py_type, void *ptr);
 #define py_talloc_import(py_type, talloc_ptr) py_talloc_import_ex(py_type, talloc_ptr, talloc_ptr)
 
 /* Sane default implementation of reprfunc. */