1

I'm making use of wxWidgets in my program for directory management and compressing/uncompromising collections of files. As I've been building my file system, I've noticed that I get memory leaks every run. After a lot of testing, I realized that any time I use any functions related to wxFileName, I get a memory leak. I'm using wx widgets 3.0.1, and my standalone example is as follows.

#include <wx\filename.h>

int main()
{
    wxFileName::Mkdir("Test");
    return 0;
}

The result is the same if I make an instance of the wxFileName class.

How do I make wx widgets not create a memory leak? I want to be able to package large collections of files in one file, and read the data from them with various other libraries (via extracting the zip to a temporary folder and reading the data from there). I haven't been able to get any other library to zip/unzip entire folders, so I really need to be able to use wxWidgets without a memory leak.

I read in another thread that the visual studios debugger is falsely identifying the memory leaks, but I ran it through AQtime and it confirmed that there was indeed a memory leak.

The exact debug output involving the memory leak is as follows:

Detected memory leaks!
Dumping objects ->
{1087} normal block at 0x009B4BC0, 64 bytes long.
 Data: <\+= d+= l+= t+= > 5C 2B 3D 00 64 2B 3D 00 6C 2B 3D 00 74 2B 3D 00 
{1086} normal block at 0x009B4880, 772 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{1085} normal block at 0x009B4680, 28 bytes long.
 Data: < H              > 80 48 9B 00 C1 00 00 00 00 00 00 00 CD CD CD CD 
Object dump complete.
Chris Hayes
  • 11,471
  • 4
  • 32
  • 47
Legacyblade
  • 335
  • 1
  • 5
  • 18
  • So *that specific code in your question* reports as leaking memory by devstudio and aqtime? Assuming you built `wx` in debug with symbols, where does devstudio say the source of the leaked allocation is? – WhozCraig Oct 02 '14 at 04:50
  • @WhozCraig yes that specific code reports a memory leak. I edited the original post to show the exact memory leak. I'm not sure what devstudio is. And I built wx in debug, but I don't know if I included symbols (would that happen by default?). Also, from what I gather, the memory leak occurs at "wxAnyValueTypeGlobals". – Legacyblade Oct 02 '14 at 05:05
  • DevStudio is what produced that output (actually, the CRT debug reporting at shutdown, but you got it from the output log of devstudio). That is truly odd. I'm actually more prone to using WinDbg and the heap analysis fathers of that tool instead of VS. – WhozCraig Oct 02 '14 at 06:29
  • 1
    Completely unrelated to your question, but writing `wx\filename.h` is not portable, use `wx/filename.h` instead. – VZ. Oct 02 '14 at 14:58

1 Answers1

1

After a bit of digging (it WOULD be the digging I did AFTER posting the question) I found that when you're using wxWidgets without creating a wxWidgets app object, you need to use the following two functions:

wxInitialize()

and

wxUninitialize()

So the fixed version of my code is as follows:

#include <wx/app.h> 

#include <wx\filename.h>

int main()
{
    wxInitialize();

    wxFileName::Mkdir("Waka Waka");

    wxUninitialize();

    return 0;
}

I suggest if anyone is using wxWidgets purely for the file management to either call these functions in the constructor and destructor of whatever class handles files, or at the beginning and end of your program's main loop.

Legacyblade
  • 335
  • 1
  • 5
  • 18
  • 1
    This is correct, you can also use `wxInitializer` class to avoid having to call `wxUninitialize()` manually. – VZ. Oct 02 '14 at 14:58