Skip to content

Commit 2b7842a

Browse files
elmoplstefanseefeld
authored andcommitted
Fix memory leaks in enum.cpp
Unfortunately due to optimised build of Python3 libraries and executable I got only partial stack from [http://clang.llvm.org/docs/AddressSanitizer.html], however digging into and reducing my code I tracked it down to be issue with `boost/libs/python/src/object/enum.cpp`. It has to bits that leak (and comment mentioning there is one): PyObject *mod = PyObject_GetAttrString( self_, "__module__"); Leaks reference, as it never decreases it. It also stores a new string object under object's `name` that ref count never gets decremented. That commit fixes both issues.
1 parent 8d37630 commit 2b7842a

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/object/enum.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ static PyMemberDef enum_members[] = {
3434

3535
extern "C"
3636
{
37+
static void
38+
enum_dealloc(enum_object* self)
39+
{
40+
Py_XDECREF(self->name);
41+
Py_TYPE(self)->tp_free((PyObject*)self);
42+
}
43+
3744
static PyObject* enum_repr(PyObject* self_)
3845
{
39-
// XXX(bhy) Potentional memory leak here since PyObject_GetAttrString returns a new reference
40-
// const char *mod = PyString_AsString(PyObject_GetAttrString( self_, const_cast<char*>("__module__")));
4146
PyObject *mod = PyObject_GetAttrString( self_, "__module__");
47+
object auto_free(handle<>(mod));
4248
enum_object* self = downcast<enum_object>(self_);
4349
if (!self->name)
4450
{
@@ -88,7 +94,7 @@ static PyTypeObject enum_type_object = {
8894
const_cast<char*>("Boost.Python.enum"),
8995
sizeof(enum_object), /* tp_basicsize */
9096
0, /* tp_itemsize */
91-
0, /* tp_dealloc */
97+
(destructor) enum_dealloc, /* tp_dealloc */
9298
0, /* tp_print */
9399
0, /* tp_getattr */
94100
0, /* tp_setattr */

0 commit comments

Comments
 (0)