Skip to content

Memory leaks in MFC dialog base application when use scoped_interpreter #2062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ruoleng opened this issue Jan 9, 2020 · 4 comments
Closed

Comments

@ruoleng
Copy link

ruoleng commented Jan 9, 2020

The example code in VC 2017 give a leak tip in debug mode.
Output windows show text like this:

Detected memory leaks!
Dumping objects ->
{266} normal block at 0x00C9B4E8, 4 bytes long.
Data: < > 00 00 00 00
Object dump complete.

I had try the code in Win32 Application, it's OK, no leak.

The version of Visual Stuido 2017 I'm using is 15.9.16.

It seem the "internals_pp" pointer did not release.
In "internals.h" file of function "inline internals **&get_internals_pp() ".

Reproducible example code

namespace py = pybind11;
py::scoped_interpreter guard{};
auto dtPassTo = py::dict();
auto global = py::dict(py::module::import("main").attr("dict"));
dtPassTo["msg"] = py::cpp_function(
[](std::string strMsg) {
::MessageBoxA(nullptr, strMsg.c_str(), "", MB_OK);
});

py::exec("msg('test')", global, dtPassTo);

@ruoleng
Copy link
Author

ruoleng commented Jan 15, 2020

Well, I did a little dig. Then I found in 'internals.h' file, the function 'PYBIND11_NOINLINE inline internals &get_internals() ' there is a code block (line 272 ~ 274 ):

    if (!internals_pp) internals_pp = new internals*();
    auto *&internals_ptr = *internals_pp;
    internals_ptr = new internals();

Find out internals_pp is a pointer that point to another pointer. But only release the *internals_pp pointer, not release the internals_pp pointer.
The release code in line 163 ~ 166 in the embed.h file, which is the block of function ‘inline void finalize_interpreter() '.

After I add release internals_pp pointer , there is not more leak.

Before fix, release code is :

if (*internals_ptr_ptr) {
delete *internals_ptr_ptr;
*internals_ptr_ptr = nullptr;
}

After fix , release code is :

if (*internals_ptr_ptr) {
delete *internals_ptr_ptr;
*internals_ptr_ptr = nullptr;
}

if (internals_ptr_ptr) {
delete internals_ptr_ptr;
internals_ptr_ptr = nullptr;
}

Sorry, for my poor English. I wish that make you clear the leak.

@erwincoumans
Copy link

erwincoumans commented Feb 28, 2020

I can confirm there is a memory leak, even without MFC.
In my case, 'finalize_interpreter' in embed.h doesn't seem to be called. Where/how is finalize_interpreter called?

if (!internals_pp) internals_pp = new internals*();

@bstaletic
Copy link
Collaborator

We know about this. Help is needed.

#2024

@bstaletic
Copy link
Collaborator

@rwgk fixed this one in #2413.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants