SFML community forums

Help => Window => Topic started by: Strelok on June 08, 2014, 11:37:24 am

Title: [SOLVED]reference to sf::RenderWindow passed to a constructor THEN initialized
Post by: Strelok on June 08, 2014, 11:37:24 am
 
OS: any SFML version: git build:DEBUG TGUI version:git build:DEBUG Error: SEGFAULT
#include <SFML/Window.hpp>
#include <TGUI/TGUI.hpp>

class Form
{
public:
    Form(sf::RenderWindow& window): m_Gui(window)
    {
        window.getSize();
    }
    ~Form() {}
private:
    tgui::Gui m_Gui;
};
class Game
{
public:
    Game()
        : m_Window(sf::VideoMode::getDesktopMode(), "...", sf::Style::Default)
        , m_Form(m_Window)
    {
        /*stuff*/
    }
    ~Game() {}
private:
    Form m_Form; //<----this is the error! Even though m_Window is initialized before m_Form
    sf::RenderWindow m_Window; //<---- the order is decided by the members' position in the header
    tgui::Gui m_Gui; //[-WReorder] or [-Wall] weren't set in my project so I didn't notice.
};

int main()
{
    Game game;
    return 0;
}
 
Title: Re: Reference to sf::RenderWindow causes Access Violation to happen.
Post by: Nexus on June 08, 2014, 12:56:21 pm
Please read this thread (http://en.sfml-dev.org/forums/index.php?topic=5559) and clarify the description accordingly. Especially the "minimal complete example" section is important.

Make sure you link everything correctly.
Title: Re: [SOLVED]reference to sf::RenderWindow passed to a constructor THEN initialized
Post by: Jesper Juhl on June 08, 2014, 06:42:42 pm
1. Do what Nexus asks.

2. Some random guesses and suggestions below:

- enable more warnings for your build and fix them.
- make sure the SFML version you are using is built for your compiler (or build it yourself if it is not).
- use tools like "address sanitizer", "valgrind" and similar to track down the bug (Google them).
- use a debugger to help track down what's going on.
- use static code analysis to track down what you are doing wrong ("coverity prevent" is awesome but commercial, "clang analyze" is free and pretty good).
- build with a different compiler (and/or different OS) and compare warnings and runtime results.

Title: Re: [SOLVED]reference to sf::RenderWindow passed to a constructor THEN initialized
Post by: Strelok on June 08, 2014, 06:57:49 pm
As you can see from the title and the code, I've already solved the problem.
Title: Re: [SOLVED]reference to sf::RenderWindow passed to a constructor THEN initialized
Post by: Jesper Juhl on June 08, 2014, 07:03:35 pm
Yeah. I saw that after making the post, but then didn't see a reason to delete it. It might help someone else.  :)
Title: Re: [SOLVED]reference to sf::RenderWindow passed to a constructor THEN initialized
Post by: Strelok on June 08, 2014, 08:34:02 pm
Yeah. I saw that after making the post, but then didn't see a reason to delete it. It might help someone else.  :)
And that's why I didn't delete it. I honestly would have never thought about something like this if it weren't for the idea of adding -Wall to the compiler's args (that should be DEFAULT on cmake DEBUG build IMO)  ;D
Title: Re: [SOLVED]reference to sf::RenderWindow passed to a constructor THEN initialized
Post by: Jesper Juhl on June 08, 2014, 09:02:38 pm
-Wall doesn't even give you all the compiler has to give. Try adding -Wextra as well (and that even doesn't give you all; read the man page for more flags). With clang you can pass -Weverything to really get everything.