Vector subscript out of range error after calling ListBox::removeAllItems

  • 2 Replies
  • 849 Views
Hi. I'm using TGUI 0.7 (VS2015 32bit) to create a filesystem browser screen for a game. This uses a ListBox and the 'DoubleClick' signal to allow the user to double click on directories to navigate the directory tree. When the user double clicks on a directory, I call ListBox::removeAllItems to clear the list and then repopulate the list with the contents of the selected directory.

After calling removeAllItems the game sometime crashes with a "vector subscript out of range" error when the user next moves the mouse. The error occurs near line 802 of ListBox.cpp due to m_hoveringItem being greater than the number of elements in m_items:

Code: (cpp) [Select]
m_items[m_hoveringItem].setTextColor(getRenderer()->m_textColor);
The following minimal code provides a example of the problem I'm experiencing:

Code: (cpp) [Select]
#include <memory>
#include <string>
#include <SFML/Graphics.hpp>
#include <TGUI/TGUI.hpp>

int main() {
sf::RenderWindow window({ 640, 480 }, "test");
tgui::Gui gui(window);
gui.setFont("DejaVuSans.ttf");

auto listBox = std::make_shared<tgui::ListBox>();
listBox->setPosition(0, 0);
listBox->setSize(400, 400);
listBox->connect("DoubleClicked", [&]() {
listBox->removeAllItems();
});

for (int i = 0; i < 10; ++i)
listBox->addItem(std::to_string(i));

gui.add(listBox);

while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
gui.handleEvent(event);
}

window.clear();
gui.draw();
window.display();
}
return 0;
}

*

texus

  • *****
  • 1612
    • View Profile
    • Texus's Blog
Thanks for reporting. The fix has been made in the latest version that you can download from github.

Thanks for the quick fix. :)