memory leak reported in Visual Studio when using wxWidgets and poppler

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Ronald
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Mon Mar 05, 2018 4:17 am

memory leak reported in Visual Studio when using wxWidgets and poppler

Post by Ronald » Fri Dec 04, 2020 4:51 am

3 possible causes:
* visual studio
* poppler
* wxwidgets

I'm not sure which one is true.
Any clue? Thanks


Env
poppler 20.11.0
wxwidgets 3.1.4
Visual Studio 2019
Windows 10
arch x64

Reproduce
Code

Code: Select all

#define WXUSINGDLL
#include <wx/log.h> // this leads to memory leak, if comment out this line, no memory leak will
#include <poppler/cpp/poppler-page.h>

int main()
{
    poppler::page * __page = nullptr;
    if (__page != nullptr)
        delete __page;
    return 0;
}
Error

Code: Select all

Detected memory leaks!
Dumping objects ->
{75} normal block at 0x00000208EC86EE70, 62 bytes long.
 Data: <D:\Dev> 43 3A 5C 55 73 65 72 73 5C 61 6C 67 5C 44 65 76 
Object dump complete.
The program '[0x1B34] TextWxWidgets.exe' has exited with code 0 (0x0).

User avatar
doublemax
Moderator
Moderator
Posts: 15849
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by doublemax » Fri Dec 04, 2020 7:57 am

Code: Select all

#include <wx/log.h> // this leads to memory leak, if comment out this line, no memory leak will
If this is the only line that's responsible for the leak, it's not related to poppler at all?

Please try removing different parts of that code and check if any other conditions must be met (e.g. WXUSINGDLL and the poppler stuff).

What happens if you add the popper code into the "minimal" wxWidgets sample?
Use the source, Luke!

Ronald
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Mon Mar 05, 2018 4:17 am

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by Ronald » Fri Dec 04, 2020 4:29 pm

Please try removing different parts of that code and check if any other conditions must be met (e.g. WXUSINGDLL and the poppler stuff).
  • no poppler, no leak in any case
  • no wxwidgets, no leak in any case
  • no WXUSINGDLL , no leak in any case
  • so the the leak condition is: WXUSINGDLL + wxWdigets + poppler

User avatar
doublemax
Moderator
Moderator
Posts: 15849
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by doublemax » Fri Dec 04, 2020 5:04 pm

please check if this makes any difference:

Code: Select all

#include <poppler/cpp/poppler-page.h>
#define WXUSINGDLL
#include <wx/wx.h>
#include <wx/log.h> // this leads to memory leak, if comment out this line, no memory leak will
Use the source, Luke!

Ronald
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Mon Mar 05, 2018 4:17 am

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by Ronald » Sat Dec 05, 2020 6:37 am

doublemax wrote:
Fri Dec 04, 2020 5:04 pm
please check if this makes any difference:

Code: Select all

#include <poppler/cpp/poppler-page.h>
#define WXUSINGDLL
#include <wx/wx.h>
#include <wx/log.h> // this leads to memory leak, if comment out this line, no memory leak will
no difference

Ronald
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Mon Mar 05, 2018 4:17 am

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by Ronald » Sat Dec 05, 2020 6:39 am

After adding wxInitialize & wxUninitialize, still leak

Code: Select all

#define WXUSINGDLL
#include <wx/wx.h>
#include <wx/log.h> // this leads to memory leak, if comment out this line, no memory leak will
#include <poppler/cpp/poppler-page.h>

int main()
{
    bool ok = wxInitialize();
    assert(ok);
    poppler::page * __page = nullptr;
    if (__page != nullptr)
        delete __page;
    wxUninitialize();
    return 0;
}

User avatar
doublemax
Moderator
Moderator
Posts: 15849
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by doublemax » Sat Dec 05, 2020 8:38 am

Then i'm out of ideas. Try using VLD to find out where exactly the leak comes from.
https://kinddragon.github.io/vld/
Use the source, Luke!

Ronald
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Mon Mar 05, 2018 4:17 am

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by Ronald » Sun Dec 06, 2020 7:04 am

@doublemax

put it in minimal.cpp, still leak

Code: Select all

#define WXUSINGDLL

//#include "wx/wxprec.h"

#include "wx/wx.h"
#include <poppler/cpp/poppler-page.h>

class MyApp : public wxApp
{
public:
    virtual bool OnInit() wxOVERRIDE;
};

class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString & title);

    void OnQuit(wxCommandEvent & event);
    void OnAbout(wxCommandEvent & event);

private:
    // any class wishing to process wxWidgets events must use this macro
    wxDECLARE_EVENT_TABLE();
};

enum
{
    Minimal_Quit = wxID_EXIT,
    Minimal_About = wxID_ABOUT
};

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
EVT_MENU(Minimal_About, MyFrame::OnAbout)
wxEND_EVENT_TABLE()

wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
    if (!wxApp::OnInit())
        return false;

    poppler::page * __page = nullptr;
    if (__page != nullptr)
        delete __page;

    MyFrame * frame = new MyFrame("Minimal wxWidgets App");

    frame->Show(true);

    return true;
}

MyFrame::MyFrame(const wxString & title)
    : wxFrame(NULL, wxID_ANY, title)
{
    SetIcon(wxICON(sample));

#if wxUSE_MENUBAR
    // create a menu bar
    wxMenu * fileMenu = new wxMenu;

    // the "About" item should be in the help menu
    wxMenu * helpMenu = new wxMenu;
    helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");

    fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");

    // now append the freshly created menu to the menu bar...
    wxMenuBar * menuBar = new wxMenuBar();
    menuBar->Append(fileMenu, "&File");
    menuBar->Append(helpMenu, "&Help");

    // ... and attach this menu bar to the frame
    SetMenuBar(menuBar);
#else // !wxUSE_MENUBAR
    // If menus are not available add a button to access the about box
    wxSizer * sizer = new wxBoxSizer(wxHORIZONTAL);
    wxButton * aboutBtn = new wxButton(this, wxID_ANY, "About...");
    aboutBtn->Bind(wxEVT_BUTTON, &MyFrame::OnAbout, this);
    sizer->Add(aboutBtn, wxSizerFlags().Center());
    SetSizer(sizer);
#endif // wxUSE_MENUBAR/!wxUSE_MENUBAR
}


// event handlers

void MyFrame::OnQuit(wxCommandEvent & WXUNUSED(event))
{
    // true is to force the frame to close
    Close(true);
}

void MyFrame::OnAbout(wxCommandEvent & WXUNUSED(event))
{
    wxMessageBox(wxString::Format
    (
        "Welcome to %s!\n"
        "\n"
        "This is the minimal wxWidgets sample\n"
        "running under %s.",
        wxVERSION_STRING,
        wxGetOsDescription()
    ),
                 "About wxWidgets minimal sample",
                 wxOK | wxICON_INFORMATION,
                 this);
}

User avatar
doublemax
Moderator
Moderator
Posts: 15849
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by doublemax » Sun Dec 06, 2020 7:23 pm

What happens if you remove this? It shouldn't make any difference.

Code: Select all

    poppler::page * __page = nullptr;
    if (__page != nullptr)
        delete __page;
I looked into the poppler header files and really can't spot anything that could allocate memory. Maybe the "leak" is a strange sideeffect of linking wrong CRTs together? Does your application link to the same CRTs as the poppler library?
Use the source, Luke!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2946
Joined: Sun Jan 03, 2010 5:45 pm

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by PB » Sun Dec 06, 2020 7:35 pm

FWIW, I observed a (much bigger) memory leak reported when adding OpenCV to the smallest possible wxWidgets application, without even calling any of OpenCV functions.

No such leak was there for its pure Win32 application equivalent. I did not find a solution.

Ronald
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Mon Mar 05, 2018 4:17 am

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by Ronald » Mon Dec 07, 2020 4:18 am

doublemax wrote:
Sun Dec 06, 2020 7:23 pm
What happens if you remove this? It shouldn't make any difference.

Code: Select all

    poppler::page * __page = nullptr;
    if (__page != nullptr)
        delete __page;
After removing the codes, if build incrementally it leaks, if rebuild no leak.
doublemax wrote:
Sun Dec 06, 2020 7:23 pm
I looked into the poppler header files and really can't spot anything that could allocate memory. Maybe the "leak" is a strange sideeffect of linking wrong CRTs together? Does your application link to the same CRTs as the poppler library?
Both are using dynamic c runtime libriary.

User avatar
doublemax
Moderator
Moderator
Posts: 15849
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by doublemax » Mon Dec 07, 2020 6:22 am

Very weird. Without finding out what's actually leaking, i think we won't make any progress. Did you try VLD?
Use the source, Luke!

Ronald
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Mon Mar 05, 2018 4:17 am

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by Ronald » Mon Dec 07, 2020 9:01 am

doublemax wrote:
Mon Dec 07, 2020 6:22 am
Very weird. Without finding out what's actually leaking, i think we won't make any progress. Did you try VLD?
Tried VLD, one line said "No memory leaks detected", another said "Detected memory leaks"
```
...
The thread 0x35e4 has exited with code 0 (0x0).
The thread 0x3670 has exited with code 0 (0x0).
The thread 0x1a84 has exited with code 0 (0x0).
The thread 0x2024 has exited with code 0 (0x0).
The thread 0x1d04 has exited with code 0 (0x0).
No memory leaks detected.
Visual Leak Detector is now exiting.
Detected memory leaks!
Dumping objects ->
{76} normal block at 0x0000016EFFAA9E40, 62 bytes long.
Data: <D:\Dev> 43 3A 5C 55 73 65 72 73 5C 61 6C 67 5C 44 65 76
Object dump complete.
The program '[6288] TextWxWidgets.exe' has exited with code 0 (0x0).
```

BTW, I also make them use the same C++ standard, still leak.

utelle
Moderator
Moderator
Posts: 1013
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by utelle » Mon Dec 07, 2020 12:37 pm

Ronald wrote:
Mon Dec 07, 2020 9:01 am
doublemax wrote:
Mon Dec 07, 2020 6:22 am
Very weird. Without finding out what's actually leaking, i think we won't make any progress. Did you try VLD?
Tried VLD, one line said "No memory leaks detected", another said "Detected memory leaks"
If you are using poppler and/or wxwidgets as DLLs, you probably included the VLD header file only in your own application. In that case VLD can only detect memory leaks in your own application. If you want VLD to be able to detect memory leaks in poppler and/or wxwidgets, you would have to compile both libraries yourself and to make sure that each of them does also include the VLD header in at least one compilation unit per DLL.

FWIW, with a properly set up wxWidgets application, console or GUI, I haven't experienced any memory leaks in wxwidgets itself.

ollydbg23
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 224
Joined: Fri Dec 12, 2008 10:31 am

Re: memory leak reported in Visual Studio when using wxWidgets and poppler

Post by ollydbg23 » Mon Dec 07, 2020 2:00 pm

PB wrote:
Sun Dec 06, 2020 7:35 pm
FWIW, I observed a (much bigger) memory leak reported when adding OpenCV to the smallest possible wxWidgets application, without even calling any of OpenCV functions.

No such leak was there for its pure Win32 application equivalent. I did not find a solution.
Hi, I think this memory leak issue bring me back to some discussion I started in year 2008, see this link:

discussion in MFC news group, some exports like Doug Harrison and Joseph M. Newcomer gives me many helps.

Hope you can understand the reasons.

Post Reply