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

Author Topic: Exception thrown when attempting to run an SFML project  (Read 2720 times)

0 Members and 1 Guest are viewing this topic.

CAExempt

  • Newbie
  • *
  • Posts: 1
    • View Profile
Exception thrown when attempting to run an SFML project
« on: April 17, 2020, 01:50:22 am »
Hi everyone,

I'm a bit of a beginner to SFML (and C++ in general).

I've been having an issue when attempting to run my SFML work:

Exception thrown at 0x77422035 (ntdll.dll) in sfmlproject.exe: 0xC0000005: Access violation writing location 0x00000004.
 

VS 2019 has pointed out that the issue lies on line 4 of my alien.cpp file:
#include "../inc/alien.h"
#include <iostream>

sf::Texture alienTexture; //Exception thrown here (0x7742...)

void alien::defineAlien(sf::Sprite& alienSprite, int alienPosX, int alienPosY) {

       
        if (!alienTexture.loadFromFile("./res/alien.png")) {
                alienTexture.loadFromFile("./res/error.png");
        }

        else {
                alienSprite.setTexture(alienTexture);
                alienSprite.setScale(sf::Vector2f(0.2f, 0.2f));
        }


       
        alienSprite.setOrigin(650.0f / 2, 650.0f / 2);

        alienSprite.setPosition(sf::Vector2f(alienPosX, alienPosY));

}

void alien::moveAlien(sf::Sprite& alienSprite) {
       
        if (distanceMoved == 6)
                changeDir = true;

       
        if (distanceMoved == -6)
                changeDir = false;

        if (changeDir == false) {
                alienSprite.move(20.f, 0.f);
                distanceMoved++;
                descend++;
        }
        else if (changeDir) {
                alienSprite.move(-20.f, 0.f);
                distanceMoved--;
                descend++;
        }

}

void alien::descendAlien(sf::Sprite& alienSprite) {
        if (descend == 12) {
                alienSprite.move(0.f, 50.f);
                descendCount++;
                descend = 0;
        }
}

void alien::checkDescend(player& player) {
        if (descendCount == 4) {
                player.playerHealth = 0;
        }
}
 

For reference: the code compiles when ran, but almost-immediately, the exception is thrown.

I've spent some time googling the issue, and reading an old thread (https://en.sfml-dev.org/forums/index.php?topic=12815.0) about a similar issue that happened to someone else. I've also double checked my property pages in my VS solution to ensure that all the associated SFML libraries are being correctly referenced (I can attach screenshots of the property pages here if you wish).

I would appreciate any help with this.

Thanks.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Exception thrown when attempting to run an SFML project
« Reply #1 on: April 17, 2020, 02:49:22 am »
It's due to that sf::Texture being global, it tries to access things that aren't ready yet when it's being constructed.

Although I can't now quickly find what code would fail (it might be loadExtensions at https://github.com/SFML/SFML/blob/master/src/SFML/Window/GlContext.cpp#L235 , it gets called eventually due to sf::Texture constructor being called, and it accesses globals, so there's the order conflict potential) or why and can't reproduce it but writing location 0x00000004 looks like attempt to write via pointer that's null.

Here's someone else's thread of similar issue years ago and answer is (and still is, I think) to not use globals because they might mess init order of SFML's own globals: https://en.sfml-dev.org/forums/index.php?topic=5654.0
« Last Edit: April 17, 2020, 03:12:52 am by FRex »
Back to C++ gamedev with SFML in May 2023