Getting an EXC_BAD_ACCES error running app template code - c++

Preliminary Details:
OS X 10.11
Xcode 7.2
SFML 2.3.2
Clang C++11
Default SFML App template
Problem:
I have just done a fresh install of SFML, I double checked and triple checked all of the required files are in the correct places.
When I build and run the stock, SFML App template for Xcode, I get a EXC_BAD_ACCESS on line 81 of this file (Line 81: window.draw(sprite); - also commented clearly):
//
// Disclamer:
// ----------
//
// This code will work only if you selected window, graphics and audio.
//
// Note that the "Run Script" build phase will copy the required frameworks
// or dylibs to your application bundle so you can execute it on any OS X
// computer.
//
// Your resource files (images, sounds, fonts, ...) are also copied to your
// application bundle. To get the path to these resource, use the helper
// method resourcePath() from ResourcePath.hpp
//
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
int main(int, char const**)
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "icon.png")) {
return EXIT_FAILURE;
}
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return EXIT_FAILURE;
}
sf::Sprite sprite(texture);
// Create a graphical text to display
sf::Font font;
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
return EXIT_FAILURE;
}
sf::Text text("Hello SFML", font, 50);
text.setColor(sf::Color::Black);
// Load a music to play
sf::Music music;
if (!music.openFromFile(resourcePath() + "nice_music.ogg")) {
return EXIT_FAILURE;
}
// Play the music
music.play();
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite); // -------------- LINE 81
// Draw the string
window.draw(text);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
What I've Tried
I have searched online (including SO posts), the common cause seems to be some kind of stack overflow. But I can't see what would cause a stack overflow in the above program with my knowledge.

If you look for this kind of error of SFML's forums, you'll see that it was always due to some old header/binary files being present on the system and being used instead of the newer ones.
Make sure that you removed all versions of SFML before installing the latest one. As per Installing SFML tutorial, you need to look in:
/usr/local/include
/usr/local/lib
/Library/Frameworks

Related

SFML render window not appearing when running program

When attempting to run a program that is intended to bring forth a render window generated through the SFML library, it will successfully compile but refuse to actually launch a window. After several hours of debugging, I am unable to find where I may have potentially gone wrong in the code, and am now beginning to think it is a problem through my IDE (CLion). Nevertheless, the code used for the render window will be provided.
int main (int argc, const char * argv[])
{
// Create the main window
sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(800, 600), "SFML window");
#ifdef SHAPE
sf::Shape s = sf::Shape::Rectangle(0, 0, 100, 100, sf::Color::Blue);
#else
sf::Image img; img.create(100, 100, sf::Color::Blue);
sf::Texture t; t.loadFromImage(img);
sf::Sprite s(t);
#endif
// Start the game loop
while (window->isOpen())
{
// Process events
sf::Event event;
while (window->pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window->close();
if (event.type == sf::Event::KeyPressed)
if (event.key.code == sf::Keyboard::Escape)
window->close();
else {
delete window;
window = new sf::RenderWindow(sf::VideoMode(800, 600), "SFML window");
}
}
// Clear screen
window->clear();
s.rotate(30.0);
window->draw(s);
// Update the window
window->display();
}
delete window;
return EXIT_SUCCESS;
}
#endif
One of the most common issue with CLion users and launching an application is that they forget to provide the necessary SFML DLLs, so the application never starts but closes with a return value 0xC0000135.
If you launch the executable directly via explorer, you'll get the more well-known message box which tells you which DLL exactly is missing.
The solution is to copy the necessary DLLs next to your executable in the CLion's output directory.

SFML - failed to create the font face

I'm working on a C++ project in WSL environment. The project includes SFML library. I have to use XLaunch to open SFML windows. I'm trying to render text by loading a font from a .ttf file. I get
Failed to load font "Graphics/fonts/fff-forward-regular.ttf" (failed to create the font face)
Here's my code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(1600, 900), "Dungeon Crawler", sf::Style::Close);
sf::RectangleShape quit_button(sf::Vector2f(300, 100));
quit_button.setOrigin(150, 50);
quit_button.setPosition(800, 600);
quit_button.setFillColor(sf::Color::Magenta);
sf::RectangleShape start_button(sf::Vector2f(360, 120));
start_button.setOrigin(180, 60);
start_button.setPosition(800, 300);
start_button.setFillColor(sf::Color::Cyan);
sf::Font pixel_font;
pixel_font.loadFromFile("Graphics/fonts/fff-forward-regular.ttf");
sf::Text quit_text("Quit", pixel_font, 30);
quit_text.setOrigin(quit_text.getLocalBounds().width/2, quit_text.getLocalBounds().height/2);
quit_text.setPosition(quit_button.getPosition());
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(quit_button);
window.draw(start_button);
window.draw(quit_text);
window.display();
}
return 0;
}
The SFML window opens and everything else seems to work just fine, but rendering fonts don't work. I have checked that the path of the .ttf is correct. Can't figure out what's causing this.

What is causing "SIGABRT" in Xcode?

I am running Xcode 11.5 on my mac OS X Catalina 10.15.3. I did all the steps to set up SFML in Xcode, and created a project with the standard SFML app template, using SFML from dylibs instead of frameworks (I couldn't get the app to build when I created it with frameworks). I haven't edited the code from the template, which contains this is main.cpp:
//
// Disclaimer:
// ----------
//
// This code will work only if you selected window, graphics and audio.
//
// Note that the "Run Script" build phase will copy the required frameworks
// or dylibs to your application bundle so you can execute it on any OS X
// computer.
//
// Your resource files (images, sounds, fonts, ...) are also copied to your
// application bundle. To get the path to these resources, use the helper
// function `resourcePath()` from ResourcePath.hpp
//
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
// Here is a small helper for you! Have a look.
#include "ResourcePath.hpp"
int main(int, char const**)
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "icon.png")) {
return EXIT_FAILURE;
}
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return EXIT_FAILURE;
}
sf::Sprite sprite(texture);
// Create a graphical text to display
sf::Font font;
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
return EXIT_FAILURE;
}
sf::Text text("Hello SFML", font, 50);
text.setFillColor(sf::Color::Black);
// Load a music to play
sf::Music music;
if (!music.openFromFile(resourcePath() + "nice_music.ogg")) {
return EXIT_FAILURE;
}
// Play the music
music.play();
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite);
// Draw the string
window.draw(text);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
The app successfully builds, but the window won't open and I get following in my Threads tab:
I've read that SIGABRT can be caused by naming errors and such. I'm not that familiar with assembly so I was wondering if anyone can see the mistake here.

Why does SFML window not show when window is in a class?

I am trying to create a Screen class for SFML, however for some reason, the application works when using the Xcode example but as soon as I put the window into it's own class, it does not work. Why is this and how would I fix it?
Here is my code (adapted form the example):
Edit:
After reading the comments, I have changed to the following code. This still does not show a screen and the program still quits.
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
class Screen{
public:
sf::RenderWindow window;
Screen(){
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
}
};
int main(int, char const**)
{
Screen* screen = new Screen();
// Set the Icon
sf::Image icon;
if (!icon.loadFromFile(resourcePath() + "icon.png")) {
return EXIT_FAILURE;
}
screen->window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
return EXIT_FAILURE;
}
sf::Sprite sprite(texture);
// Create a graphical text to display
sf::Font font;
if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
return EXIT_FAILURE;
}
sf::Text text("Hello SFML", font, 50);
text.setFillColor(sf::Color::Black);
// Load a music to play
sf::Music music;
if (!music.openFromFile(resourcePath() + "nice_music.ogg")) {
return EXIT_FAILURE;
}
// Play the music
music.play();
// Start the game loop
while (screen->window.isOpen())
{
// Process events
sf::Event event;
while (screen->window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
screen->window.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
screen->window.close();
}
}
// Clear screen
screen->window.clear();
// Draw the sprite
screen->window.draw(sprite);
// Draw the string
screen->window.draw(text);
// Update the window
screen->window.display();
}
return EXIT_SUCCESS;
}
You have a logic error in your constructor. The way you created it, you leave Screen::window uninitialized, and create another RenderWindow object which becomes inaccessible as soon as execution leaves constructor.
Instead, you should do
class Screen{
public:
sf::RenderWindow window;
Screen():
window(sf::VideoMode(800, 600), "SFML window") {}
};
Everything else should work as expected, unless it does not have any other bugs. If you're curious about the syntax I've used, you can visit this link.
Hard to say. Maybe your accessing a memory address that isn't part of your program? Try using unique pointers and make_unique to define some of your pointers.
For example:
std::unique_ptr<sf::RenderWindow> window;
window = std::make_unique<sf::RenderWindow>(sf::VideoMode(WIDTH, HEIGHT), "PushBlox",sf::Style::Default);
I did make a project using sfml before, so feel free to check out my code here --> https://github.com/FromAlaska/ComputerScience/tree/2017/Projects/Frontier

SFML won't open a window?

So, as the title suggests, I am trying to create a simple Window using SFML 1.6 in CodeBlocks (MinGW v.4.7.0) on Windows 7 (no, I'm not using an ATI GPU).
This is the code:
#include <SFML/Window.hpp>
int main()
{
sf::Window App(sf::VideoMode(800, 600, 16), "SFML Window");
App.Display();
return 0;
}
Whenever I try to run this code, it just says Program.exe is not responding and has to be shutdown using Close this program. The funny thing is, the first tutorial offered on the SFML tutorial website (the one utilizing sf::Clock in a console) works, so the libraries are loaded properly.
Can someone help me find the cause of the error I'm getting?
Other than the crash, I get no compiler or application errors.
The problem is that you haven't created the main loop which polls for events and handles OS messages. Append this to main() (yes, this is a snippet from the SFML documentation):
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Clear the screen
App.Clear();
// Put your update and rendering code here...
// Update the window
App.Display();
}
It is therefore not necessary for you to call App.Display() after you create the window.
For those who want the whole thing, this is a snippet extracted from the SFML website.
#include <SFML/Window.hpp>
int main()
{
sf::Window window(sf::VideoMode(800, 600), "SFML Window");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
}
return 0;
}
You will get: