2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 04:22:16 +00:00

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.
This commit is contained in:
Krzysztof Trzciński
2017-10-24 11:09:23 +01:00
committed by Stefan Seefeld
parent 8d37630cc8
commit 2b7842a39f

View File

@@ -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)
{
@@ -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 */