Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can't create the sf::Drawable vector[error -1073741819]  (Read 952 times)

0 Members and 2 Guests are viewing this topic.

MN1R

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Can't create the sf::Drawable vector[error -1073741819]
« on: July 09, 2023, 03:53:48 pm »
Hello, everyone!
I'm the begginer in the using of SFML and there is an error, which i can't fix:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <memory>
#include <vector>

std::vector<std::shared_ptr<sf::Drawable>> createDrawableVector() {
        std::vector<std::shared_ptr<sf::Drawable>> vec;

        std::shared_ptr<sf::Font> tahoma = std::make_shared<sf::Font>();
        if (!tahoma->loadFromFile("corbel.ttf")) {
                return vec;
        }

        auto text = std::make_shared<sf::Text>("Clicks", *tahoma, 20);
        text->setPosition(100.f, 100.f);
        vec.push_back(std::move(text));

        auto rect = std::make_shared<sf::RectangleShape>(sf::Vector2f(100.f, 100.f));
        rect->setPosition(200.f, 200.f);
        rect->setFillColor(sf::Color::Red);
        vec.push_back(std::move(rect));

        return vec;
}

int main() {
        sf::RenderWindow window(sf::VideoMode(550, 700), "SFML Drawable Vector", sf::Style::Close);

        std::vector<std::shared_ptr<sf::Drawable>> drawableVector = createDrawableVector();

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

                window.clear();

                for (auto &element : drawableVector) {
                        window.draw(*element);
                }

                window.display();
        }

        return 0;
}
When i'm running it i'm taking the -1073741819 error.

But when i'm replacing it into main() the error is going away
int main() {
        sf::RenderWindow window(sf::VideoMode(550, 700), "SFML Drawable Vector", sf::Style::Close);

        std::vector<std::shared_ptr<sf::Drawable>> vec;

        std::shared_ptr<sf::Font> tahoma = std::make_shared<sf::Font>();
        if (!tahoma->loadFromFile("corbel.ttf")) {
                return -1;
        }

        auto text = std::make_shared<sf::Text>("Clicks", *tahoma, 20);
        text->setPosition(100.f, 100.f);
        vec.push_back(std::move(text));

        auto rect = std::make_shared<sf::RectangleShape>(sf::Vector2f(100.f, 100.f));
        rect->setPosition(200.f, 200.f);
        rect->setFillColor(sf::Color::Red);
        vec.push_back(std::move(rect));

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

                window.clear();

                for (auto &element : vec) {
                        window.draw(*element);
                }

                window.display();
        }

        return 0;
}
The same code with sf::RectangleShape, not sf::Text is working great
I think, that some text data became null after createDrawableVector() was finished, therefore text can't be rendered.
Cuz this code is not working too
#include <SFML/Graphics.hpp>
#include <iostream>
#include <memory>
#include <vector>

std::vector<sf::Text> createDrawableVector() {
        std::vector<sf::Text> vec;

        sf::Font corbel;
        if (!corbel.loadFromFile("corbel.ttf")) {
                return vec;
        }

        sf::Text text("Clicks", corbel, 20);
        text.setPosition(100.f, 100.f);
        vec.push_back(text);

        return vec;
}

int main() {
        sf::RenderWindow window(sf::VideoMode(550, 700), "SFML Drawable Vector", sf::Style::Close);

        std::vector<sf::Text> drawableVector = createDrawableVector();

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

                window.clear();

                for (auto &element : drawableVector) {
                        window.draw(element);
                }

                window.display();
        }

        return 0;
}
 
Please help me with it)
« Last Edit: July 09, 2023, 04:23:44 pm by MN1R »

MN1R

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Can't create the sf::Drawable vector[error -1073741819]
« Reply #1 on: July 09, 2023, 04:38:21 pm »
I've got it, just need to put font as a function argument: const sf::Font &font

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Can't create the sf::Drawable vector[error -1073741819]
« Reply #2 on: August 22, 2023, 01:16:56 pm »
I'm not sure what the other platform reply is about. But for the original problem, the issue in the first code is that the font is loaded and stored in a shared pointer. But Text objects don't understand shared pointers, they use normal pointers to store the font.
This means at the end of the createDrawableVector function, the font shared pointer only has 1 reference, so it deletes the font when the variable tahoma goes out of scope. But the Text objects in the vector still have normal pointers to where the font was.