Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
elmopl authored and stefanseefeld committed Oct 25, 2017
commit 2b7842a39f023e2aa2b401089dfeac0172844574
12 changes: 9 additions & 3 deletions src/object/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ static PyMemberDef enum_members[] = {

extern "C"
{
static void
enum_dealloc(enum_object* self)
{
Py_XDECREF(self->name);
Py_TYPE(self)->tp_free((PyObject*)self);
}

static PyObject* enum_repr(PyObject* self_)
{
// XXX(bhy) Potentional memory leak here since PyObject_GetAttrString returns a new reference
// const char *mod = PyString_AsString(PyObject_GetAttrString( self_, const_cast<char*>("__module__")));
PyObject *mod = PyObject_GetAttrString( self_, "__module__");
object auto_free(handle<>(mod));
enum_object* self = downcast<enum_object>(self_);
if (!self->name)
{
Expand Down Expand Up @@ -88,7 +94,7 @@ static PyTypeObject enum_type_object = {
const_cast<char*>("Boost.Python.enum"),
sizeof(enum_object), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
(destructor) enum_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
Expand Down