summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2009-07-01 14:05:17 +1000
committerAndrew Tridgell <tridge@samba.org>2009-07-01 15:15:37 +1000
commit956b5a0003a3ab82d2d7cffb7aee6e5281b4fbb4 (patch)
treeb31ce346b11cbada4b39dd63134c6a6feb8fe903 /lib
parent2d981919b8dd63655a39ccaa4fd7bdb39d1830f6 (diff)
downloadsamba-956b5a0003a3ab82d2d7cffb7aee6e5281b4fbb4.tar.gz
samba-956b5a0003a3ab82d2d7cffb7aee6e5281b4fbb4.tar.bz2
samba-956b5a0003a3ab82d2d7cffb7aee6e5281b4fbb4.zip
fixed use of reference in pytalloc
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
Diffstat (limited to 'lib')
-rw-r--r--lib/talloc/pytalloc.c26
-rw-r--r--lib/talloc/pytalloc.h1
2 files changed, 24 insertions, 3 deletions
diff --git a/lib/talloc/pytalloc.c b/lib/talloc/pytalloc.c
index 30da9ee5c2..3ce49d6d61 100644
--- a/lib/talloc/pytalloc.c
+++ b/lib/talloc/pytalloc.c
@@ -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);
}
diff --git a/lib/talloc/pytalloc.h b/lib/talloc/pytalloc.h
index c5a1428b29..00282c4ba0 100644
--- a/lib/talloc/pytalloc.h
+++ b/lib/talloc/pytalloc.h
@@ -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. */