The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Memory leak in ShowMessageBox?
GenuineCorruption


Joined: 21 Oct 2014
Posts: 4
Location: Canada
Surprised
Hello,
I'm pretty new to SDL, and to C/C++ for that matter, but I'm pretty sure the SDL_ShowMessageBox function has a small memory leak (or at least on Windows).
The following reproduces it:

Code:

void alert(string title, string text)
{
SDL_MessageBoxData data;
data.flags = SDL_MESSAGEBOX_INFORMATION;
data.window = caturria::window;
data.title = title.c_str();
data.message = text.c_str();
data.numbuttons = 1;
SDL_MessageBoxButtonData buttons[1];

buttons->flags = SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
buttons->buttonid = 1;
buttons->text = "Okay";
data.colorScheme = NULL;
data.buttons = buttons;
int buttonid;

      SDL_ShowMessageBox(&data, &buttonid);//Since this function returns void and creates a box with only one button I don't really need to capture the button ID, but it's here just in case the function doesn't care for NULL pointers.



}


int main(int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) == -1)
return -1;
while(true)
alert("Test", "This will leak memory... or at least it will under windows.");
}


I wrote that function before I discovered the SDL_ShowSimpleMessageBox function, but calling that repeatedly still slowly leaks memory.
Based on my testing, this program should consume 1.2 - 1.3 MB of memory as of the SDL_Init call; after 1000 ShowMessageBox calls it's up to roughly 2 MB.
This is specific to Windows and I don't have anything else to test it on. I don't have enough knowledge to dive in to SDL's code itself (though I did try looping some function calls made by the ShowMessageBox function itself and didn't find something obvious to me). Just setting up the SDL_MessageBoxData and SDL_MessageBoxButtonData structures and then trapping just the SDL_ShowMessageBox call in a loop gives the same result, so it must be in that function or one of the other functions it calls during it's execution.
Is this a known issue, or is there something obvious that I'm doing wrong to cause memory to be leaked?

Kind regards,

Jordan.[/code]
brada


Joined: 16 Aug 2012
Posts: 38
I'm not saying there *isn't* a leak, but the method you took to arrive at that conclusion is flawed.

Now I'm not a "Windows" guy, but I think its true for any OS that there are any number of non-leaky reasons why memory usage doesnt recede back to ~1.3. You need to use an actual profiling tool like Valgrind to check for actual leaks.
GenuineCorruption


Joined: 21 Oct 2014
Posts: 4
Location: Canada
Hello,
True it's not an extensive diagnostic test... but is there a reason why the message box feature should cause the application's memory usage to ascend without ever coming back down?
I could totally see the first call resulting in some permanent memory usage if there was some initialization involved, but short of that I don't see why showing a message box would need to allocate memory and have it persist for the lifetime of the application.
Admittedly I'm pretty green when it comes to lower level programming where memory management comes in to play, but I would suspect this has gone unnoticed due to the fact that a real-world need to show thousands of message boxes during the course of a multimedia application's lifetime doesn't exist in all but the most obscure of cases.
brada


Joined: 16 Aug 2012
Posts: 38
Quote:
is there a reason why the message box feature should cause the application's memory usage to ascend without ever coming back down


it could be as simple as the memory manager (heap manager?) deciding its more efficient to let the process retain ownership of the memory in case it needs to do more allocations. if your system were starving for memory it probably would return the memory to the system. There are other reasons too I think, but really the point is that tests like this can often indicate leaks, but to actually know you should use a tool designed for detecting leaks (Valgrind). Operating systems are big complicated beasts where processes are caching things, getting messages from other processes, and who-knows-what-else without you (as both a user and a programmer) being aware.

Quote:
I would suspect this has gone unnoticed due to the fact that a real-world need to show thousands of message boxes during the course of a multimedia application's lifetime


maybe. I cant testify to what the SDL team does, but if they are regularly probing for leaks then all it takes is a single tiny leak to spot this. Of course that assumes they have a test which incorporates a message box. I havent checked out SDLs test suite, but I hear its pretty extensive. However, message boxes probably contain a fair amount of platform dependent code, so maybe it only leaks for Windows as you suggest.

As I said before, I'm not saying there isn't a leak, just that somebody is going to have to fire up a Valgrindesque tool and check to both determine if there is one, and find what and where it is leaking.