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

Author Topic: [CMake, MSVC, win8] Application crashes when it starts  (Read 11654 times)

0 Members and 1 Guest are viewing this topic.

Mrowqa

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
[CMake, MSVC, win8] Application crashes when it starts
« on: March 31, 2013, 11:27:00 am »
Hi!

I tried compile and run simply SFML application by MSVC and GCC and I have strange problem.

When I manually configured projects (Visual Studio 11 and CodeBlocks 12.11), app works. So, I wanted to add CMakeLists.txt to my current project and I got problem. When I generate makefiles/solution and compile app by GCC (static or dynamic linked) or Visual Studio dynamic linked, it works, but when I try generate Visual Studio 11 solution, compile it and run - I got this:
Debug: http://puu.sh/2r8eo
Release: http://puu.sh/2r8f8

I'm using win8 and SFML 9fac5d74dcef2e78b21c8500589ea9f8cf263fb3 (Jan 16, 2013). For finding SFML I'm using attached FindSFML.cmake:
# find SFML and add include path
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML 2 COMPONENTS audio graphics window system REQUIRED)
include_directories(${SFML_INCLUDE_DIR})
# ...
# add the executable
add_executable(CM_Billiard ${CM_BILLIARD_SRC})
target_link_libraries(CM_Billiard ${SFML_LIBRARIES} ${BOX2D_LIBRARIES})
 

How to correct it?

Regards,
Mrowqa
« Last Edit: March 31, 2013, 12:09:44 pm by Mrowqa »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [CMake, MSVC, win8] Application crashes when starting
« Reply #1 on: March 31, 2013, 11:35:39 am »
Do you have global variables in your project?

Could you try a minimal code, to know whether it's your configuration/environment or your code which causes the problem?
Laurent Gomila - SFML developer

Mrowqa

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: [CMake, MSVC, win8] Application crashes when starting
« Reply #2 on: March 31, 2013, 12:01:52 pm »
I don't have global variables AFAIR, but I have many static (in class). Official repo: https://bitbucket.org/Mrowqa/cm-billiard - any of these modules haven't been using, but I compile all code. Actual CM_Billiard.cpp:
#include <iostream>
#include "CMBilliardConfig.h"
using namespace std;

int main2();

int main()
{
        cout << "Source code in development\n"
                        "Actual version: " << CM_BILLIARD_VERSION_MAJOR << "." << CM_BILLIARD_VERSION_MINOR << "." << CM_BILLIARD_VERSION_EXTRA_INFO << "\n";
        main2();
        return 0;
}

#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>

int main2()
{
        b2World world(b2Vec2());
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

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

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}
 

PS code on repositorium can't be compile by GCC. It works with MSVC (locally I corrected code, but not uploaded).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #3 on: March 31, 2013, 07:17:29 pm »
Quote
I don't have global variables AFAIR, but I have many static (in class).
Statics are initialized at global scope too. What SFML classes are involved?

And...
Quote from: Laurent
Could you try a minimal code, to know whether it's your configuration/environment or your code which causes the problem?
Laurent Gomila - SFML developer

Mrowqa

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #4 on: April 02, 2013, 03:34:36 pm »
I know that I have static sf::RenderWindow - in global scope app can't create new object. I tested sf::Window, sf::RenderTexture, sf::Context, sf::Image and sf::Time too. Three first of them crashes app. That's probably sf::NonCopyable constructor's blame.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #5 on: April 02, 2013, 03:53:49 pm »
No that's because statics are global and thus don't have a well defined destruction order and a few other quirks. Just use a better code design, that doesn't require globals, especially not ones like sf::RenderWindow. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #6 on: April 02, 2013, 03:54:06 pm »
Quote
That's probably sf::NonCopyable constructor's blame.
Nop, it's more complicated. It's because they are OpenGL resources.

Anyway: don't use globals!
Laurent Gomila - SFML developer

Mrowqa

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #7 on: April 02, 2013, 04:40:04 pm »
Thanks! Someday maybe I'll learn OpenGL, but now it doesn't make sens for me - there's SFML :)

Just use a better code design, that doesn't require globals, especially not ones like sf::RenderWindow. ;)
I wanna singleton for sf::RenderWindow (I'll make static pointer). I'm not professional programmer and I'm still learning how to make good code design :)

Nop, it's more complicated. It's because they are OpenGL resources.

Anyway: don't use globals!
App compiled by GCC works, so... It's strange for me for the time being. MSVC...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #8 on: April 02, 2013, 04:51:20 pm »
Quote
Someday maybe I'll learn OpenGL, but now it doesn't make sens for me - there's SFML
App compiled by GCC works, so... It's strange for me for the time being. MSVC...
It has nothing to do with OpenGL. It's just about the initialization order of globals: it is not defined by the C++ standard, it's up to the compiler to decide. Therefore your own globals can be initialized before SFML globals with gcc, and the opposite with VC++.
The problem is that OpenGL classes in SFML (sf::RenderWindow, sf::Context, ...) need a special treatment that requires the use of a global variable in their constructor. So if you construct an instance of one of these classes at global scope, before SFML globals are initialized, SFML ends up using an uninitialized variable and it can crash. And this is exactly what happens.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #9 on: April 02, 2013, 06:12:33 pm »
I wanna singleton for sf::RenderWindow (I'll make static pointer). I'm not professional programmer and I'm still learning how to make good code design :)
When do you want to learn it? Some day in the far future?

You can start improving your code style now. Try to write code without a single global or static variable, and without a singleton. It may be difficult in the beginning, but it's worth the trouble. Especially because you begin to think about dependencies, and because you are forced to structure your program, making the single parts more modular and maintenance-friendly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mrowqa

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #10 on: April 02, 2013, 09:50:33 pm »
As I wrote previously, I'm still learning. I didn't know that global/static variables are so bad for good code design. I've written one module for global managing resources (based on static variables), so there's long way to go through for me ::) Thanks for advice!

XxSpeedDemonxXx

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #11 on: April 03, 2013, 12:23:45 am »
Quote
As I wrote previously, I'm still learning. I didn't know that global/static variables are so bad for good code design. I've written one module for global managing resources (based on static variables), so there's long way to go through for me  Thanks for advice!

Global variables are never a good thing. Use classes, data structures, and enums to hold your variables. Declare your variable inside a data container then define them in a function held inside a class. Also, use abstract classes. Here's an example:

// Class.h //
class IDClass {
public:
    virtual void function(void) = 0;
    virtual void function2(void) = 0;
};
extern IDclass* idclass;

// Class.cpp //
class IDClassLocal : public IDClass {
public:
    virtual void function(void);
    virtual void function2(void);
private:
    int integer;
    bool boolean;
};
IDClassLocal idclassLocal;
IDClass* idclass = &idclassLocal;

void IDClassLocal::function(void) {
    integer = 5;
    boolean = (false);
}
void IDClassLocal::function2(void) {
    integer = 2;
    boolean = (true);
}

// main.cpp //
int main() {
    idclass->function(void); // integer = 5, boolean = (false)
    // Other code
    idclass->function2(void) // integer = 2, boolean = (true)
}
 

The following shows the abstract class being declared in a header, then being inherited and defined in the cpp. This is polymorphism at it's best. Using this style will get rid of a lot of issues. If you have an issue you can't seem to solve, run your program in debug mode under a debugger; doing this, you should be able to find the cause.
From sanity, there is insanity.

~Atomic Platypus Studios~

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #12 on: April 03, 2013, 04:45:34 am »
Also, use abstract classes. Here's an example:
To be honest, I've no idea how this fits into the topic at hand... ::)
We all know by now that he's still learning and might even be reading a good book about C++, but such a small, uncommented and not clean, regarding code design, example won't teach him much.

The following shows the abstract class being declared in a header, then being inherited and defined in the cpp. This is polymorphism at it's best. Using this style will get rid of a lot of issues. If you have an issue you can't seem to solve, run your program in debug mode under a debugger; doing this, you should be able to find the cause.
You sound like you're selling the solution to all problems. ;D
Programming in C++ is much more than a bit polymorphism and debugging. Besides that, it's a better code design to avoid polymorphism where ever possible, or better said, use it only when really needed and not as general formula.
« Last Edit: April 03, 2013, 04:47:22 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mrowqa

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #13 on: April 03, 2013, 08:19:41 am »
Good knowledge about language is base. I read really good book about C++ by George Grębosz some years ago, but people are learning all life :) I'll look for good book about code design. Thanks again ;)

XxSpeedDemonxXx

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: [CMake, MSVC, win8] Application crashes when it starts
« Reply #14 on: April 03, 2013, 09:43:21 am »
Quote
such a small, uncommented and not clean, regarding code design, example won't teach him much

Well, it's pretty straight forward and I honestly didn't think it would require comments. Considering that's a similar piece of code out of "Doom 3"'s source, I wouldn't call it "unclean", considering "Doom 3" had some of the cleanest, most efficient code ever written.

How does it fit in this topic? Containers for data, which was the main issue in the program he wrote. - Preventing the usage of global variables.

EDIT: Also, polymorphism is the base of object oriented programming, so saying:
Quote
it's a better code design to avoid polymorphism where ever possible
would be saying to avoid O.O.P., would it not?

Quote
or better said, use it only when really needed and not as general formula.
Doesn't that go for almost all techniques?

Programming is much more than polymorphism and debugging, but we are not talking about the definition of programming; we are talking about good programming structure and efficient, effective code. My example also showed pointer, abstract class, and inheritance usage. Not only that, but the usage of a class to store functions and variables in general.
« Last Edit: April 03, 2013, 11:14:14 am by XxSpeedDemonxXx »
From sanity, there is insanity.

~Atomic Platypus Studios~

 

anything