raysan5 / raygui

A simple and easy-to-use immediate-mode gui library
zlib License
3.61k stars 303 forks source link

Use of GuiIconText() leads to confusing side-effects due to static buffer. #442

Closed markusbkk closed 1 week ago

markusbkk commented 1 week ago

I spent the past few hours investigating this, after my own little application consistently drew the wrong icon and text for a WindowBox.

I use C++ and built my own little dialog classes with a .name property.

When I tried setting their name via

  configDialog.name = GuiIconText(ICON_GEAR, "Settings");
  objectDialog.name = GuiIconText(ICON_CUBE, "Add Object");

I was surprised to find that configDialog ended up displaying the text and icon meant for objectDialog.

Is there a particular reason we can't just do

    char* buffer = (char*)malloc(1024);
    char* iconBuffer = (char*)malloc(16);

here?

raysan5 commented 1 week ago

@markusbkk raygui is an immediate-mode library and consequently most data life-span is usually restricted to frame. GuiIconText() returns a static string to be used at that same moment, if that string needs to be stored, that's up to the user.

Consequently, you should manage the memory allocations on user side, because raygui does not allocate data dynamically.

Solution:

configDialog.name = (char *)calloc(1024);
strcpy(configDialog.name, GuiIconText(ICON_GEAR, "Settings"));
...
free(configDialog.name);