Maybe it's a stupid question but I don't understand why I can catch the exception in the former piece of code, while I can't in the latter.
First piece of code:
int main() {
try {
throw std::logic_error("Error");
sf::RenderWindow window(sf::VideoMode(800, 600), "Test", sf::Style::Default);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
window.display();
}
}
catch(std::logic_error& l){
std::cerr<<l.what()<<std::endl;
exit(42);
}
return 0;
}
Second piece of code:
int main() {
try {
sf::RenderWindow window(sf::VideoMode(800, 600), "Test", sf::Style::Default);
throw std::logic_error("Error");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
window.display();
}
}
catch(std::logic_error& l){
std::cerr<<l.what()<<std::endl;
exit(42);
}
return 0;
}
At the end I've discovered that the problem regarded how I had linked the SFML library.
I just had to remove -static from target_link_libraries(SFML_Test sfml-system sfml-window sfml-graphics -static) in the CMake file.
Thank you to everyone who has tried to help me.
Probably, because sf::RenderWindow throws exception of another type earlier.
Try catching std::exception, not std::logic_error (it's inherited from std::exception)
Update
Probably your app is crashing during sf::RenderWindow initialization. In this case exception is not generated, but you can try to catch signal.
#include <signal.h>
void fall()
{
int* p = 0x00000000;
*p = 13;
}
void posix_death_signal(int signum)
{
signal(signum, SIG_DFL);
exit(3);
}
int main(int argc, char *argv[])
{
signal(SIGSEGV, posix_death_signal);
fall();
return 0;
}
Or maybe std::abort is called from sf::RenderWindow, so you can try to handle it: https://en.cppreference.com/w/cpp/utility/program/abort
Or std::unexpected. You can try to handle it also
https://en.cppreference.com/w/cpp/error/unexpected
https://en.cppreference.com/w/cpp/error/set_unexpected
std::exit...
Actually I would like to advice attach debugger and see what happens, see how far is running your app before returning
Update 2
Sometimes the root problem is missing DLL which prevents app from starting.
Related
I'm currently trying to use TGUI with SFML as the backend, everything works fine when I had this code
#include <iostream>
#include <TGUI/TGUI.hpp>
int main() {
sf::RenderWindow window{ {800, 600}, "TGUI window with SFML" };
tgui::GuiSFML gui{ window };
gui.loadWidgetsFromFile("menus/startMenu.txt");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
gui.handleEvent(event);
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
gui.draw();
window.display();
}
}
However, I then tried to add a line to refer to a button loaded from tgui::Button::Ptr aButton = gui.get<tgui::Button>("a");.
#include <iostream>
#include <TGUI/TGUI.hpp>
int main() {
sf::RenderWindow window{ {800, 600}, "TGUI window with SFML" };
tgui::GuiSFML gui{ window };
gui.loadWidgetsFromFile("menus/startMenu.txt");
tgui::Button::Ptr aButton = gui.get<tgui::Button>("a"); // right here
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
gui.handleEvent(event);
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
gui.draw();
window.display();
}
}
and it gives me this error
Exception thrown at 0x7956271B (tgui.dll) in maze 2.exe: 0xC0000005: Access violation reading location 0x000003E4
I'm currently dynamically linking tgui and sfml, using TGUI-0.9 and SFML-2.5.1 with Debug x86 on Visual Studio c++.
The error also tells me it's coming from
template <class T>
typename T::Ptr get(const String& widgetName) const
{
return std::dynamic_pointer_cast<T>(get(widgetName));
}
in Container.hpp in TGUI.
I think that the problem is the dynamic_pointer_cast throwing an error, but I don't know how to fix it. I also don't understand why everything else works except the gui.get<typename>("sometext"); function. Any help?
Edit 1: I've gone ahead and tested with gui.get(), which works perfectly fine. This means that the problem is definitely in the dynamic_pointer_cast, since gui.get<typename>() just calls gui.get() and runs dynamic_pointer_cast on it.
Ok so I just had to restart Visual studio, and it now works perfectly
I'm trying to learn C++. I'm trying to get into GUI-programming with the frame work SFML (based on OpenGL) and TGUI. When I run my basic program (just window initialisation and creating a button) in Visual Studio (debug mode), it throws the exception: Access violation reading location 0xCCCCCCCC. I found out, that 0xCCCCCCCC is a specific memory pattern and means I am doing stuff with uninitialised memory on the stack. But where am I doing this in my program? My Code:
#include <TGUI/Gui.hpp>
#include <TGUI\TGUI.hpp>
#include <TGUI\Widgets\Button.hpp>
using namespace std;
int main() {
tgui::Button::Ptr button = tgui::Button::create("Test"); //HERE THE EXCEPTION IS THROWN
sf::RenderWindow window(sf::VideoMode(800, 600), "Fenster");
tgui::Gui gui(window);
gui.add(button, "Hallo");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// When the window is closed, the application ends
if (event.type == sf::Event::Closed)
{
window.close();
} else if (event.type == sf::Event::Resized){
window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
gui.setView(window.getView());
}
// Pass the event to all the widgets
gui.handleEvent(event);
}
window.clear();
// Draw all created widgets
gui.draw();
window.display();
}
delete button;
button = NULL;
return EXIT_SUCCESS;
}
Any help would be nice. Thanks!
I'm attempting to get a picture to display by using SFML (just a test run). The program can find the picture, and open a new window, but when it opens the window it only pops up for half a second then returns with 1. Here is the code (which is just their example that I tweaked):
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
sf::Texture Texture;
sf::Sprite Sprite;
if(!Texture.loadFromFile("resources/pepe.png"));
return 1;
Sprite.setTexture(Texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(Sprite);
window.display();
}
return 0;
}
I am assuming the error is coming from the return 1; after the loading, but I don't see what is wrong. Can someone post something that worked for them or give me tips on what may be going wrong?
Your code works just fine, except for the ; after the texture loading from a file, making your program always return 1, whatever was happening before.
It's a good idea to add error messages to know what's going wrong.
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
sf::Texture Texture;
sf::Sprite Sprite;
if(!Texture.loadFromFile("resources/pepe.png")){ // there was a ; here.
// making the code below always run.
std::cerr << "Error loading my texture" << std::endl;
return 1;
}
Sprite.setTexture(Texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed){
window.close();
}
// you only get here when there is at least one event.
}
// but you always want to display to the screen.
window.clear();
window.draw(Sprite);
window.display();
}
return 0;
}
My rule of thumb is to always enclose code blocks with curly braces so you never make these kind of mistakes (or someone else changing your code is less prone to make that mistake).
I'm trying to open a SFML window, but every time it is launched it says "Access violation reading location: 0xCCCCCCC0." The error is occuring in the init() method. Relevant code:
class AirportGame {
private:
sf::RenderWindow window;
public:
void init();
int run();
/
void AirportGame::init() {
window.create(sf::VideoMode(800, 600), "SFML window");
}
int AirportGame::run() {
init();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
return 0;
}
int main() {
AirportGame* app = new AirportGame();
return app->run();
}
It happens sometime after init, because the actual window is open. There is no mention in the debugger of 0xCCCCCC0.
Fixed it!
Turns out under the C++ pre-processor I set the definition to SFML_STATIC instead of SFML_DYNAMIC
Set window to a
RenderWindow *window;
and create it with
window = new sf::RenderWindow( /*your stuff or default initialize*/ );
and then call
window->create( /*your settings*/ );
if you didn't already initialize it.
From then on just access window using '->' instead of '.'
I'm new to SFML and am currently reading through the documentation. For some reason, however, my "program" crashes whenever I call the Clear() function. My program code is as follows:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <iostream>
void HandleEvents(sf::RenderWindow &App)
{
sf::Event Event;
while (App.GetEvent(Event)) {
if (Event.Type == sf::Event::Closed) {
App.Close();
exit(0);
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) {
App.Close();
exit(0);
}
}
}
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 640, 32), "SFML Graphics");
App.SetFramerateLimit(60); // cap the framerate at 60 FPS
while (App.IsOpened()) {
HandleEvents(App); // handle events... duh
App.Clear(); // When I remove this line, the program doesn't crash....
App.Display();
}
return EXIT_SUCCESS;
}
So do any SFML gurus have advice for me? I've tried researching this problem but the only other person I've seen who tried to ask this had a broken link (because of the new SFML forums).
Thanks.