From: Andrew Tridgell Date: Wed, 1 Jul 2009 04:05:17 +0000 (+1000) Subject: fixed use of reference in pytalloc X-Git-Url: http://git.samba.org/?p=metze%2Fsamba%2Fwip.git;a=commitdiff_plain;h=956b5a0003a3ab82d2d7cffb7aee6e5281b4fbb4 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 --- diff --git a/lib/talloc/pytalloc.c b/lib/talloc/pytalloc.c index 30da9ee5c2b9..3ce49d6d61fc 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 c5a1428b29d9..00282c4ba0e9 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. */