-
I am trying to display a Text object and I am receiving an error when I try sending it to the draw function in my sf::RenderWindow object. Bear in mind that I was originally a java programmer so I might be doing something that only works in java (but I doubt it).
The exception looks like (though I doubt it will do any good because it seems like it is sending me the location in the runtime memory the exception is occuring):
Exception thrown at 0x548EAB3F (sfml-graphics-d-2.dll) in Survior.exe: 0xC0000005: Access violation reading location 0xCCCCCD24. occurred
//where exception in relayed
void TextInteraction::textDisplay() {
//displays regular text white
MainWindow::window.draw(*text);
}
//constructor
TextInteraction::TextInteraction(RECT box, std::string _text, sf::Font font, int size, float x, float y) : HitBox(box) {
text = new sf::Text(_text, font, size);
text->setOrigin(x, y);
text->setColor(sf::Color::White);
}
-
Your sf::Font object you're passing to the constructor is taking a copy, which no longer exists at the end of the constructor. Make it a const reference and you should be fine.
-
So I made the constructor require a const reference of my sf::Font object, but I am still receiving the same exception as before mentioned.
TextInteraction::TextInteraction(RECT box, std::string _text, const sf::Font &font,
int size, float x, float y) : HitBox(box) {
text = new sf::Text(_text, font, size);
text->setPosition(x, y);
text->setOrigin(x, y);
text->setColor(sf::Color::White);
}
-
I was hoping that sfml's classes would copy construct the variables I pass to it (cuz I am a newb), and it actually saves memory by requiring the user to make sure the variables that they send to the object have been saved by the user (not just deleted after scope aka a very java way of programming). I have to leave my safe space of OOP.
[You don't need to read the next sentence]
I don't need to const reference but I might as well just leave it there for good luck because either way it isn't going to do anything either way.
TextInteraction::TextInteraction(RECT box, const std::string &pText, const sf::Font &_font,
const int &_size, float x, float y) : HitBox(box) {
content = new std::string(pText);
font = new sf::Font(_font);
size = new int(_size);
text = new sf::Text(*content, *font, *size);
text->setPosition(x, y);
text->setOrigin(x, y);
text->setColor(sf::Color::White);
}
-
0xC0000005 usually indicates that you're mixing x86 and x64 modes, either by using the wrong runtime DLLs or by using the wrong SFML DLL. Makr sure that you use the runtime libs provided by your compiler.