简体   繁体   English

使用 sfml,调用 window.close() 给出:*** 检测到堆栈粉碎 ***:已终止

[英]Using sfml, calling window.close() gives: *** stack smashing detected ***: terminated

The program was running fine until I added the Peca class.该程序运行良好,直到我添加了 Peca 类。 Currently still runs until I call window.close().目前仍在运行,直到我调用 window.close()。 I have sfml 2.5.1 installed and I'm using Ubuntu 20.04.我安装了 sfml 2.5.1,我使用的是 Ubuntu 20.04。 Removing all references to the Peca class eliminates the problem, and so does removing window.close().删除对 Peca 类的所有引用消除了这个问题,删除 window.close() 也是如此。 I read that having multiple versions of sfml might cause this, but I only seem to have one.我读到有多个版本的 sfml 可能会导致这种情况,但我似乎只有一个。

#include "Game.h"

int main(){
    Game game;
    game.run();
    return 0;
}
#include "Game.h"
#define TIMEPERFRAME sf::seconds(1.f/60.f)
#define GAMENAME "game name" 

Game::Game()
:    window(sf::VideoMode(500,600),GAMENAME, sf::Style::Default),
     peca()
{    
}

void Game::update(sf::Time timePerFrame){    
}

void Game::render(){    
}

void Game::run(){
    sf::Clock clock;
    sf::Time elapsedSinceUpdate = sf::Time::Zero;        

    while(window.isOpen()){
        processEvents();
        elapsedSinceUpdate += clock.restart();
        while(elapsedSinceUpdate > TIMEPERFRAME){
            elapsedSinceUpdate -= TIMEPERFRAME;
            processEvents();
            update(TIMEPERFRAME);
        }
        render();
    }

}

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

#include "Peca.h"

Peca::Peca()
    :tile(4)
{
    tile.setPoint(0, sf::Vector2f(0, 0));
    tile.setPoint(1, sf::Vector2f(20, 10));
    tile.setPoint(2, sf::Vector2f(40, 0));
    tile.setPoint(3, sf::Vector2f(20, -10));

}

sf::ConvexShape Peca::getTile(){
    return tile;
}

Headers:标题:

#ifndef __GAME__
#define __GAME__

#include <SFML/Graphics.hpp>
#include "Peca.h"  

class Game{
    
    public:
        Game();
        void run();

    private:
        void processEvents();
        void update(sf::Time timePerFrame);
        void render();

        sf::RenderWindow window;
        Peca peca;

};

#endif
#ifndef __PECA__
#define __PECA__

#include <SFML/Graphics.hpp>

class Peca{
    public:
        Peca();
        sf::ConvexShape getTile();

    private:
        sf::ConvexShape tile;

};

#endif

makefile:生成文件:

run: jogo
    ./jogo

jogo: jogo.o Game.o Peca.o
    g++ jogo.o Game.o Peca.o -o jogo -lsfml-graphics -lsfml-window -lsfml-system

jogo.o: jogo.cpp
    g++ -c jogo.cpp

Game.o: Game.cpp
    g++ -c Game.cpp

Peca.o: Peca.cpp
    g++ -c Peca.cpp

sfml version: sfml 版本:

$ apt list libsfml-dev 
libsfml-dev/focal,now 2.5.1+dfsg-1build1 amd64 [installed]

Tried to use gdb and got this:尝试使用 gdb 并得到这个:

Core was generated by `./jogo'.
Program terminated with signal SIGABRT, Aborted.
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
[Current thread is 1 (Thread 0x7feb93078680 (LWP 27482))]
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007feb9388d859 in __GI_abort () at abort.c:79
#2  0x00007feb938f83ee in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7feb93a2207c "*** %s ***: terminated\n") at ../sysdeps/posix/libc_fatal.c:155
#3  0x00007feb9399ab4a in __GI___fortify_fail (msg=msg@entry=0x7feb93a22064 "stack smashing detected") at fortify_fail.c:26
#4  0x00007feb9399ab16 in __stack_chk_fail () at stack_chk_fail.c:24
#5  0x000055e2081cf4f3 in main ()

Firstly im on windows but tested using ubuntu on wsl.首先我在 Windows 上,但在 wsl 上使用 ubuntu 进行了测试。

I stopped using make files as i always made errors in them so moved over to cmake.我停止使用 make 文件,因为我总是在其中出错,因此转移到 cmake。

My CMakeLists.txt for sfml is as follows:我的 sfml 的 CMakeLists.txt 如下:

cmake_minimum_required(VERSION 3.16)

project(StackOverflow)

# Windows specific config
IF (WIN32)
    # Include local sfml cmake config
    set(SFML_DIR "${CMAKE_CURRENT_SOURCE_DIR}/SFML-2.5.1/lib/cmake/SFML")
    # Link sfml statically (Optional)
    set(SFML_STATIC_LIBRARIES TRUE)
ENDIF()

find_package(SFML 2.5.1 COMPONENTS graphics audio REQUIRED)

FILE(
    GLOB
    SOURCES
    "src/*.cpp"
)

add_executable(StackOverflow ${SOURCES})

target_link_libraries(StackOverflow sfml-graphics sfml-audio)

target_include_directories(StackOverflow
    PRIVATE
    "${PROJECT_BINARY_DIR}"
    "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

The directory is like so目录是这样的

./
L include/
    L game.h
    L peca.h
L src/
    L game.cpp
    L peca.cpp
CMakeLists.txt

Running跑步

$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./StackOverflow

runs without error运行没有错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM