SFML/imgui-sfml

stack smashing detected

giorgi43 opened this issue · 3 comments

has anyone noticed stack smashing detected error after closing SFML window? i am using SFML and imgui-sfml for my simple application. i rechecked code many times and still not found what might be the cause. most annoying thing is, it does not always appear and i am unable to reproduce it every time. there is not going much in actual program yet, it just draws grid, and imgui windows.
here is how this error might appear.. one time, i changed some files of code, compiled code with running make, then run program, when i close window, it may show stack smashing detected error, and if error is shown, after consequent runs, program always shows this error. if i recompile from scratch again (running make clean and make), open program, close window, error no longer appears. as i checked error happens on ImGui::SFML::Shutdown() but could not dig down further. could this be bug related to imgui-sfml? or maybe my makefile does not work as intended, i mean, its mixing old .o object files and new ones that was updated.. because, after my limited observation, error appears when i am not building from scratch (say i changed one file and compiled) but maybe i am wrong. i know it is specific with my app and is hard for you to tell exactly without reading actual code, but maybe you as well had similar situation...

Hello
It's really hard to say without looking at the code. You should find a minimal example which reproduces this behavior - you can do it by subtracting part of the code until you are left with ImGui-SFML stuff mostly.
Then you can post it here so I can take a look. Otherwise it's not possible to help you.

Please also share your OS, desktop environment if using Linux and what IDE/build system you're using.

Hello, i think that part of code would give you general idea.. I derive from Application class to make concrete class for my program, and overload init(), update(), and processEvents() functions.. i only handle sf::Event::Close event in processEvents() by closing m_window and mouse scroll event to zoom in/out. this event callbacks are set in init() (i use custom event manager for convenience). in update() function i have only this ImGui code below and drawing simple sfml rectangle (actually, custom sf::Drawable grid) i also use sf::View for zoom in and out. nothing else happens in the program yet, so its actually small but scattered in different .hpp and .cpp files.

ImGui::Begin();
ImGui::Button("Button");
ImGui::End();

When i need actual m_window to be used in derived class and by its member objects, i am able to access/pass it around from derived class directly because its in protected segment. i pass it as reference to objects when i need it somewhere else.

Application class

#pragma once
#include "imgui.h"
#include "imgui-SFML.h"
// other includes...

namespace pfv
{
    template <typename Derived>
    class Application
    {
    public:
        Application() : m_window(sf::VideoMode(conf::window_width, conf::window_height), "Application")
        {
            m_window.setFramerateLimit(60);
            ImGui::SFML::Init(m_window);
        }

        ~Application() {}

        void run() {
            init();

            sf::Clock deltaClock;

            while (m_window.isOpen()) {
                processEvents();
                ImGui::SFML::Update(m_window, deltaClock.restart());
                m_window.clear();

                update(); // rendering of imgui windows and actual "game" stuff happens here

                ImGui::SFML::Render(m_window);
                m_window.display();
            }

            ImGui::SFML::Shutdown(); // i had this line also in destructor
        }
    
    protected:

        // to initialize app specific resources
        void init() {
            static_cast<Derived*>(this)->init();
        }

        // called on every frame
        void update() {
            static_cast<Derived*>(this)->update();
        }

        // called on every frame
        void processEvents() {
            static_cast<Derived*>(this)->processEvents();
        }

    protected:
        sf::RenderWindow m_window;

    private:
        static void throwRuntimeError(const char* err) {
            spdlog::error("Error: {}", err);
            throw(std::runtime_error(err));
        }

    };
}

I am on Ubuntu 22.04 Gnome 42.1, SFML is installed from Ubuntu's repos. for build, i use g++ with Makefile that i took from one of /imgui/examples/ and tweaked for SFML and imgui-sfml. does run() looks ok to you? as said in docs, window closes before ImGui::SFML::Shutdown().
Other thing to note. in main() i create derived Application class called MyApp on the stack like this:

MyApp app;
app.run();

in this case if error happens, it says Stack smashing detected.
on the other hand, i had situation where i allocated MyApp on the heap, and when error happened, text was related but different, Double free or memory corruption (could not remember exact wording of error) certainly there is something wrong with memory usage in my app but still can not discover it.. if all that info is not helpful to you, i will try to reproduce it on more minimal and concrete example but as i said its hard for me as well... Thanks

UPDATE: Switched from Makefile to CMake and problem seems to be gone. i think, "wrongly" written Makefile was problem, in short, not all necessary source files were recompiled after changing code that caused misbehavior and crash in my case. at least that is my explanation and what others suggested as well.