Wednesday, 16 December 2020

How to fix "swig/python detected a memory leak of type 'HWND *', no destructor found"?

Consider this mcve:

mcve.h

#pragma once

#include <windows.h>

class Windows {
  public:
    static HWND GetActiveWindow();
    static HWND GetForegroundWindow();
};

mcve.cpp

#include "mcve.h"

HWND Windows::GetActiveWindow() {
    return ::GetActiveWindow();
}
HWND Windows::GetForegroundWindow() {
    return ::GetForegroundWindow();
}

mcve.i

%module mcve

%include <std_string.i>
%include <std_vector.i>
%include <typemaps.i>
%include <windows.i>
%include <cpointer.i>
%include <carrays.i>

%{
#include "mcve.h"
%}


%include "mcve.h"

setup.py

from distutils.core import Extension
from distutils.core import setup

setup(
    name="mcve",
    ext_modules=[
        Extension(
            "_mcve",
            sources=["mcve.i", "mcve.cpp", "mcve_wrap.cpp"],
            swig_opts=["-c++", "-python"],
            include_dirs=["."],
            library_dirs=[
              "D:/software/vcpkg/installed/x86-windows/lib"
            ],
            libraries=["user32"]
        )
    ],
    py_modules=["mcve"],
)

In order to run it, make sure you adjust properly the library_dirs variable and then just type:

python setup.py build_ext --inplace

If the extension has been generated succesfully then you just run python test.py and you should see an output similar to this:

>python test.py
swig/python detected a memory leak of type 'HWND *', no destructor found.
swig/python detected a memory leak of type 'HWND *', no destructor found.

I know you can use swig -nodefaultdtor to not generate implicit default destructors, I've tried and that didn't make any difference.

For the sake of completeness, here's roughly how the HWND typedefs would look like:

typedef PVOID HANDLE;
typedef HANDLE HWND;
typedef void *PVOID;

QUESTION: What's the proper way in swig to prevent that memory leak of HWND?



from How to fix "swig/python detected a memory leak of type 'HWND *', no destructor found"?

No comments:

Post a Comment