【问题标题】:Getting an EXC_BAD_ACCES error running app template code运行应用程序模板代码时出现 EXC_BAD_ACCES 错误
【发布时间】:2016-04-22 09:22:46
【问题描述】:

初步细节:

  • OS X 10.11
  • Xcode 7.2
  • SFML 2.3.2
  • Clang C++11
  • 默认 SFML 应用程序模板

问题:

我刚刚完成了 SFML 的全新安装,我反复检查并检查了所有必需的文件都在正确的位置。

当我为 Xcode 构建和运行股票、SFML 应用程序模板时,我在该文件的第 81 行得到一个 EXC_BAD_ACCESS(第 81 行:window.draw(sprite); - 也明确注释):

//
// 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;
}

我的尝试

我在网上搜索过(包括SO帖子),常见的原因似乎是某种堆栈溢出。但据我所知,我看不出是什么导致上述程序中的堆栈溢出。

【问题讨论】:

  • 您确定平台库在运行时对您的应用程序可见吗?当您将库链接到可执行文件时,它可能在那里,但不在可执行文件夹中。这通常在应用程序尝试访问不再存在或不在应用程序内存边界内的内存位置时引发。
  • 忘记最后一句话...你能画出文字吗(注释掉draw(sprite)?

标签: c++ xcode macos sfml


【解决方案1】:

如果您在 SFML 的论坛中查找此类错误,您会发现这始终是由于系统上存在一些旧的头文件/二进制文件并被使用而不是较新的文件。

确保在安装最新版本之前删除了所有版本的 SFML。根据Installing SFML 教程,您需要查看:

  • /usr/local/include
  • /usr/local/lib
  • /Library/Frameworks

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多