Skip to content

Memory leak when destructing a scoped_interpreter. #1151

@tareqsiraj

Description

@tareqsiraj

Issue description

Embedding the python interpreter seems to leak memory when compiled with address sanitizer. This happens even with just constructing and destructing a scoped_iterpreter. This seems to happen with both system python2 and python3 libraries.

Environment

g++ (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406

Reproducible example code

//  g++ -std=c++14 -Ipybind11/include -I/usr/include/python3.5m  -fsanitize=address leak.cpp /usr/lib/x86_64-linux-gnu/libpython3.5m.so -o leak && ./leak

#include <pybind11/embed.h>

int main() {
  pybind11::scoped_interpreter _;
  return 0;
}

Expected output

Actual output

=================================================================
==32415==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 385376 byte(s) in 175 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1af69f  (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xa169f)

Direct leak of 32704 byte(s) in 146 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1afd3a in PyDict_New (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xa1d3a)

Direct leak of 15701 byte(s) in 24 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1732a7  (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0x652a7)

Direct leak of 12320 byte(s) in 1 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1af4f9 in _PyDict_NewPresized (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xa14f9)

Direct leak of 568 byte(s) in 1 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1736ac  (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0x656ac)

Direct leak of 288 byte(s) in 3 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1b0044  (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xa2044)

Direct leak of 224 byte(s) in 1 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1af5f9 in _PyDict_NewPresized (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xa15f9)

Direct leak of 128 byte(s) in 1 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1b6942 in _PyObjectDict_SetItem (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xa8942)

Direct leak of 72 byte(s) in 5 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1ff686 in PyList_New (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xf1686)

Direct leak of 48 byte(s) in 1 object(s) allocated from:
    #0 0x7ff3dc864270 in __interceptor_realloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc7270)
    #1 0x7ff3dc204564  (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xf6564)

Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc17efc8 in PyModule_Create2 (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0x70fc8)

Direct leak of 3 byte(s) in 2 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc32c8b1 in PyCode_New (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0x21e8b1)

Direct leak of 2 byte(s) in 2 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc178f64 in PyModule_ExecDef (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0x6af64)

Indirect leak of 42576 byte(s) in 48 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1732a7  (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0x652a7)

Indirect leak of 2816 byte(s) in 22 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1b0cba in _PyDict_NewKeysForClass (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xa2cba)

Indirect leak of 1664 byte(s) in 4 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1af69f  (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0xa169f)

Indirect leak of 648 byte(s) in 1 object(s) allocated from:
    #0 0x7ff3dc863ec0 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc6ec0)
    #1 0x7ff3dc1736ac  (/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0+0x656ac)

SUMMARY: AddressSanitizer: 495162 byte(s) leaked in 438 allocation(s).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions