Search code examples
c++graphicssfmlaccess-violation

SFML/C++ | Drawing text from a vector gives access violation error


I'm making my own text editor with C++ and the SFML multi-media library. I'm now trying to implement a user interface so users can interact and select documents to edit. I have this function:

std::vector<sf::Text> Menu::GetAllTextOptions(TextHandler& handler, sf::Font font) {
std::vector<TextDocument>* all_available_documents = handler.GetTextDocuments();
std::vector<sf::Text> menu_text_options;

for (int i = 0; i < all_available_documents->size(); i++) {
    sf::Text text;
    text.setFont(font);
    text.setString(std::to_string(i) + ") " + all_available_documents->at(i).GetDocumentName() + '\n');
    menu_text_options.push_back(text);
}
return menu_text_options;   
}

What this is supposed to do is collect all of the names of already existing text documents in a path that I have specified (the code behind gathering the TextDocument objects works perfectly). It then makes a text object, sets a font to it and sets the string to the name of the document. When I std::cout the count of the vector, it displays correctly the amount of documents that exist.

I then have a function that is supposed to go through the vector of texts and draw all of them, so they can be later displayed:

// ACCESS VIOLATION ERROR
void Menu::DrawAllOptions(sf::RenderWindow& window, std::vector<sf::Text>& 
menu_text_options) {
std::cout << menu_text_options.size() << " is the current size " << std::endl;
try {
    for (int i = 0; i < menu_text_options.size(); i++) {
        // EXCEPTION THROWN HERE
        window.draw(menu_text_options.at(i));
    }
}
catch (const std::exception& ex) {

    std::cout << "Exception: " << ex.what() << '\n';
 }
} 

Finally, I have the main loop where everything comes into place:

std::string Menu::MenuControl(TextHandler& handler) {
Utility utility;
sf::RenderWindow menu_window(sf::VideoMode(800, 600), "Menu");
sf::Color backgroundColor(0, 32, 63);
sf::Event event;
sf::Font font;
sf::Text menu_text;
utility.CheckFontLoaded(font, "../8bitfont.ttf");

std::vector<sf::Text> menu_text_options = GetAllTextOptions(handler, font);
int menu_counter = 0; // Start at the first document, count goes up and down 
with arrows

menu_text.setOrigin(sf::Vector2f(-20.f, 0.f));
menu_text.setFont(font);

while (menu_window.isOpen()) {
    // DRAWING
    menu_window.clear(backgroundColor); // Setting menu background color
    //window.draw(background_overlay);
    
    DrawAllOptions(menu_window, menu_text_options);

    // DISPLAYING
    menu_window.display();

    // TO DO - ABSTRACT USER INPUT LOGIC
    while (menu_window.pollEvent(event)) {

        switch (event.type)
        {
            case sf::Event::Closed:
                menu_window.close();
                return "Test";
                break;

            case sf::Event::TextEntered:
                if (event.text.unicode < 128) {

                }
                break;

            case sf::Event::KeyReleased:
                break;
        }

    }
}
return "Not correctly working";

}

The exact error I get is:

Exception thrown at 0x00007FFA3E2869E9 (sfml-graphics-d-2.dll) in texteditor.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

At this point, I've attempted putting a try catch block, initializing the vector with new and making everything within the main loop function. I am sensing its something to do with the Text objects that I push into the vector, but I tried initializing them differently as well with no success. Any suggestions would be greatly appreciated!


Solution

  • The answer to the problem, as mentioned by @Botje, is that the function Menu::GetAllTextOptions was getting a copy of font, which ultimately ended up not being loaded and resulting in an access violation.