Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]reference to sf::RenderWindow passed to a constructor THEN initialized  (Read 4134 times)

0 Members and 1 Guest are viewing this topic.

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
 
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;
}
 
« Last Edit: July 11, 2014, 11:55:15 am by Strelok »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Reference to sf::RenderWindow causes Access Violation to happen.
« Reply #1 on: June 08, 2014, 12:56:21 pm »
Please read this thread and clarify the description accordingly. Especially the "minimal complete example" section is important.

Make sure you link everything correctly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
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.


Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
As you can see from the title and the code, I've already solved the problem.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Yeah. I saw that after making the post, but then didn't see a reason to delete it. It might help someone else.  :)

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
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

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
-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.

 

anything