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

Author Topic: Access violation when trying to load texture from file.  (Read 2988 times)

0 Members and 1 Guest are viewing this topic.

Sphynxinator

  • Newbie
  • *
  • Posts: 11
    • View Profile
Access violation when trying to load texture from file.
« on: February 15, 2018, 05:58:10 pm »
Hi, I recently faced an access violation error. I was wrapping the sf::Texture and sf::Sprite classes into a GameObject class. Here is the data structure:

>Bitmaps
  >System
     >system_logo.png
>Framework
  >System
    >EventManager
    >GameObject
    >GUIElement
    >MusicObject
    >SoundObject
    >WindowManager
>Stages
  >LanguageSelect
  >Stage
  >Title
>main.cpp

main.cpp
#include "Framework/System/WindowManager/WindowManager.h"
#include "Framework/System/EventManager/EventManager.h"
#include "Framework/System/GameProperties/GameProperties.h"
#include "Framework/System/InputManager/InputManager.h"
#include "Stages/Stage/Stage.h"
#include "Stages/LanguageSelect/LanguageSelect.h"
#include "Stages/Title/Title.h"

int main()
{
        GameProperties gameProperties;

        WindowManager windowManager(640, 360, "Rabid Streets", false);
        EventManager eventManager;
        InputManager inputManager;

        Stage* currentStage;

        bool languageSelected = false;

        if (languageSelected == false) {
                currentStage = &LanguageSelect();
        }
        else {
                currentStage = &Title();
        }

        if (!currentStage->Initialize()) {
                //return 0;
        }

        while (windowManager.WindowIsOpen()) {

                while (windowManager.PollEvents(eventManager.GetEvents())) {
                        if (eventManager.IsLostFocus()) {
                                gameProperties.SetIsGamePaused(true);
                        }
                }

                currentStage->Render();

                windowManager.ClearWindow();

                currentStage->Draw();

                windowManager.DisplayWindowContents();
        }

        currentStage->Destroy();

    return 0;
}


 

LanguageSelect.cpp
#include "LanguageSelect.h"
#include "../../Framework/System/WindowManager/WindowManager.h"

LanguageSelect::LanguageSelect() {

}

bool LanguageSelect::Initialize() {
        //logo is initialized
        sf::Texture texLogo;
        if (!texLogo.loadFromFile("Bitmaps/System/system_logo.png")) {
                return false;
        }
        logo.SetTexture(texLogo);
        logo.SetGameObjectPosition(320, 50);

        return true;
}

void LanguageSelect::Render() {

}

void LanguageSelect::Draw() {
        logo.Draw(&WindowManager::GetWindow());
}

void LanguageSelect::Destroy() {
       
}
 

LanguageSelect.h
#ifndef LANGUAGESELECT_H
#define LANGUAGESELECT_H

#include "../Stage/Stage.h"
#include <SFML/Graphics.hpp>
#include "../../Framework/System/GUIElement/GUIElement.h"

class LanguageSelect : public Stage {
public:
        LanguageSelect();
        bool Initialize();
        void Render();
        void Draw();
        void Destroy();
private:
        GUIElement logo;
};

#endif
 

GUIElement.cpp
#include "GUIElement.h"
#include <iostream>

GUIElement::GUIElement() {
       
}

void GUIElement::SetGameObjectPosition(float xP, float yP) {
        x = xP;
        y = yP;

        RefreshPosition();
}

void GUIElement::Draw(sf::RenderWindow* window) {
        window->draw(*sprite);
}

void GUIElement::SetTexture(sf::Texture& tex) {
        texture = &tex;

        std::cout << texture << std::endl;

        sprite->setTexture(tex);

        sprite->setOrigin(sf::Vector2f(sprite->getTexture()->getSize().x * 0.5f, sprite->getTexture()->getSize().y * 0.5f));
}

void GUIElement::SetSpriteRect(int rX, int rY, int rWidth, int rHeight) {
        sprite->setTextureRect(sf::IntRect(rX, rY, rWidth, rHeight));
}

void GUIElement::SetX(float value) {
        x = value;

        RefreshPosition();
}

void GUIElement::SetY(float value) {
        y = value;

        RefreshPosition();
}

float GUIElement::GetX() {
        return x;
}

float GUIElement::GetY() {
        return y;
}

void GUIElement::Move(float xP, float yP) {
        x += xP;
        y += yP;

        RefreshPosition();
}

void GUIElement::SetColor(sf::Color color) {
        sprite->setColor(color);
}

void GUIElement::RefreshPosition() {
        sprite->setPosition(sf::Vector2f(x, y));
}
 

GUIElement.h
#ifndef GUIELEMENT_H
#define GUIELEMENT_H

#include <SFML/Graphics.hpp>

class GUIElement {
public:
        GUIElement();
        void SetGameObjectPosition(float xP, float yP);
        void Draw(sf::RenderWindow* window);
        void SetTexture(sf::Texture& tex);
        void SetSpriteRect(int rX, int rY, int rWidth, int rHeight);
        void SetX(float value);
        void SetY(float value);
        float GetX();
        float GetY();
        void Move(float x, float y);
        void SetColor(sf::Color color);
protected:
        sf::Texture* texture;
        sf::Sprite* sprite;
        float x;
        float y;
private:
        void RefreshPosition();
};

#endif
 

This function gives error:

void GUIElement::SetTexture(sf::Texture& tex) {
        texture = &tex;

        std::cout << texture << std::endl;

        sprite->setTexture(tex);

        sprite->setOrigin(sf::Vector2f(sprite->getTexture()->getSize().x * 0.5f, sprite->getTexture()->getSize().y * 0.5f));
}
 

This line:
sprite->setTexture(tex);
 

Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Access violation when trying to load texture from file.
« Reply #1 on: February 15, 2018, 06:14:12 pm »
You never create the sprite, but you try to access it, causing undefined behavior and consequently the access violation.

There's no need to use a pointer for the sprite.

Additionally I advise you to use a resource holder like Thor implements one or similar, instead of combining the texture and sprite in one object.

http://www.bromeon.ch/libraries/thor/tutorials/v2.0/resources.html
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sphynxinator

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Access violation when trying to load texture from file.
« Reply #2 on: February 15, 2018, 06:25:41 pm »
How to create the sprite? I deleted pointers and now they are pure types.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Access violation when trying to load texture from file.
« Reply #3 on: February 15, 2018, 06:35:09 pm »
The sprite has a default constructor.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sphynxinator

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Access violation when trying to load texture from file.
« Reply #4 on: February 15, 2018, 06:54:50 pm »
Yes but you said that I didn't create the sprite. If it has a default constructor, why the program didn't create it? I fixed the problem with this approach, but I don't know if this is an expensive thing to process for RAM.

void GUIElement::Draw(sf::RenderWindow& window) {
        sf::Sprite sprite;
        sprite.setTexture(texture);
        sprite.setColor(sf::Color(255, 255, 255));
        window.draw(sprite);
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Access violation when trying to load texture from file.
« Reply #5 on: February 15, 2018, 07:52:44 pm »
A point is just a pointer, it's not an object until you create it. Once you remove the pointer and just have it as normal member, then it will be default initialized.

Recreating the sprite every time you draw certainly works and it's not really an issue, but kind of wasteful as well, if you can just have it as member variable.

To me it seems, that you're still lacking quite a bit of C++ experience, especially in the object oriented programming area, as such I'd recommend you spend some time in understanding what classes, objects, instances, pointers, stack allocated objects, heap allocated objects, smart pointers, references, etc. are.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sphynxinator

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Access violation when trying to load texture from file.
« Reply #6 on: February 15, 2018, 08:33:50 pm »
Yes I am lack of experience about C++. Thanks, I'll try. ^^

 

anything