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

Author Topic: access violation with unique pointer (c++)  (Read 11510 times)

0 Members and 1 Guest are viewing this topic.

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
access violation with unique pointer (c++)
« on: June 14, 2014, 11:40:54 pm »
hi,
i am working on a 2D shooter. As i wanted to rework the code because its just spaghetti, i caused an error i cant fix myself and hope that someone of you guys can help me ;)

i will post the code that i think will be relevant for solving the issue. if you need something more, please tell me.

//main.cpp
#include "Game.h"

int main()
{
    Game pew;
    pew.ChangeState(Game::gameStates::MAINMENU);

    pew.Run();

    return 0;
}

//Game.h

#ifndef GAME_H
#define GAME_H

#include <iostream>
#include <memory>

#include "GameState.h"
#include "MainMenuState.h"
#include "PlayState.h"
#include "Intro.h"

class Game
{
public:
     Game();
    ~Game();

    enum class gameStates{ MAINMENU, INTRO, SETTINGS, PLAY, GRAPHICSET, SOUNDSET, DIFFSET, COOPSET, HIGHSCORE };

    void Run();
    void ChangeState(gameStates newState);
    bool isRunning(){ return running; };
    void setRunning(bool mRunning);
   
    sf::RenderWindow window;

private:

    void Quit();
    void Update();
    void HandleEvents();
    void Render();

    std::unique_ptr<GameState> CurrentState;
   
    bool running;
};

#endif

//Game.cpp

#include "Game.h"

Game::Game()
{
    running = true;

    window.create(sf::VideoMode(800, 600), "Pew");
    window.setFramerateLimit(60);
    window.setVerticalSyncEnabled(true);
}

Game::~Game()
{
    CurrentState = nullptr;
}

void Game::Run()
{
    while (running)
    {
        Update();
        HandleEvents();
        Render();
        Quit();    
    }
}

void Game::ChangeState(gameStates newState)
{
    switch (newState)
    {
    case gameStates::MAINMENU:
        CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState));
        break;
    case gameStates::PLAY:
        CurrentState = std::move(std::unique_ptr<PlayState>(new PlayState));
        break;
    case gameStates::INTRO:
        CurrentState = std::move(std::unique_ptr<Intro>(new Intro));
        break;
    }
}

void Game::setRunning(bool mRunning)
{
    running = mRunning;
}

void Game::Quit()
{
    if (!running)
        window.close();
}
void Game::Update()
{
    CurrentState->Update(*this);
}
void Game::HandleEvents()
{
    CurrentState->HandleEvents(*this);
}
void Game::Render()
{
    window.clear();
    CurrentState->Render(*this);
    window.display();
}
 

//MainMenuState.h

#ifndef MAINMENUSTATE_H
#define MAINMENUSTATE_H

#include "Game.h"
#include "MenuSfx.h"
#include "IOstuff.h"

class MainMenuState : public GameState
{
public:
    MainMenuState();
    ~MainMenuState();
    void HandleEvents(Game &game);
    void Update(Game &game);
    void Render(Game &game);
private:
    IOsound iosound;
    MenuSound sound;

    bool  playing;
    float speed;
    float elapsedTime;
    float x_movement;
    float y_movement;
    float x, y;
    short int   debauch;
    short int   selection;
    int   volume;

    sf::Event   *pEvent;
    sf::Clock   *pClock;
    sf::Texture *texture;
    sf::Sprite  *sprite;

    Background bg;
    Text play, again, settings, close;
};

#endif
 

//MainMenuState.cpp

#define PI 3.14159
#include "MainMenuState.h"
#include <cmath>

MainMenuState::MainMenuState()
{
    bg.setFilePath("graphics//core//menu.png");
   
    //sound & music
    sound.LoadSoundBuffer();
    sound.setBuffer(volume);

    playing = false;
    speed = 0.5;
    selection = 0;
    iosound.ReadSoundSettings(volume);

    pEvent = new sf::Event;
    pClock = new sf::Clock;
    texture = new sf::Texture;
    sprite = new sf::Sprite;

    //Enemy
    elapsedTime = 0;
    x_movement = 0;
    y_movement = 0;
    debauch = 200;
    texture->loadFromFile("graphics//enemies//enemy.png");
    texture->setSmooth(false);
    sprite->setTexture(*texture);
    sprite->setOrigin(36.5, 35);

    //buttons

    play.setStringAndSize("play", 70);
    again.setStringAndSize("again", 70);
    settings.setStringAndSize("settings", 70);
    close.setStringAndSize("close", 70);

    play.setPosition(270, 150);
    again.setPosition(270, 150);
    settings.setPosition(270, 250);
    close.setPosition(270, 350);
}

MainMenuState::~MainMenuState()
{
    delete pEvent;
    delete pClock;
    delete sprite;
    delete texture;

    pEvent =  nullptr;
    pClock =  nullptr;
    sprite = nullptr;
    texture = nullptr;

}

void MainMenuState::HandleEvents(Game &game)
{
    while (game.window.pollEvent(*pEvent))
    {
        if (pEvent->type == sf::Event::Closed)
            game.setRunning(false);

        //Keyboard selection
        if (pEvent->type == sf::Event::KeyPressed)
        {
            switch (pEvent->key.code)
            {
            case sf::Keyboard::Up:
                if (selection > 0)
                {
                    selection -= 1;
                    sound.PlaySound("select");
                }
                else
                    selection = 0;
                break;

            case sf::Keyboard::Down:
                if (selection < 2)
                {
                    selection += 1;
                    sound.PlaySound("select");
                }
                else
                    selection = 2;
                break;

            case sf::Keyboard::Return:
                if (selection == 0)
                {
                    if (playing)
                        game.ChangeState(Game::gameStates::PLAY);
                    else
                        playing = true;
                        game.ChangeState(Game::gameStates::INTRO);
                }
                else if (selection == 1)
                    game.ChangeState(Game::gameStates::SETTINGS);
                else
                    game.setRunning(false);
                break;
            default:
                break;
            }
        }
    }
}

void MainMenuState::Update(Game &game)
{
    if (selection == 0)//Spielen
    {
        play.setColor(sf::Color(255, 128, 0));
        again.setColor(sf::Color(255, 128, 0));
        settings.setColor(sf::Color(255, 255, 255));
        close.setColor(sf::Color(255, 255, 255));
    }
    else if (selection == 1)//Settings
    {
        play.setColor(sf::Color(255, 255, 255));
        again.setColor(sf::Color(255, 255, 255));
        settings.setColor(sf::Color(255, 128, 0));
        close.setColor(sf::Color(255, 255, 255));
    }
    else//Beenden
    {
        play.setColor(sf::Color(255, 255, 255));
        again.setColor(sf::Color(255, 255, 255));
        settings.setColor(sf::Color(255, 255, 255));
        close.setColor(sf::Color(255, 128, 0));
    }

    //moving enemy
    elapsedTime = pClock->restart().asMilliseconds();
    x_movement += elapsedTime;
    y_movement += elapsedTime;
    y = sprite->getPosition().y;
    x = sprite->getPosition().x;
    y = 300 + std::sin((y_movement * PI) / 180) * debauch;
    x = 400 + std::cos((x_movement * PI) / 180) * debauch;

    if (x_movement > 360)
        x_movement = 0;

    if (y_movement > 360)
        y_movement = 0;

    sprite->setPosition(x, y);
}

void MainMenuState::Render(Game &game)
{
    bg.Render(game.window);
    game.window.draw(*sprite);

    if (playing)
        again.Render(game.window);
    else
        play.Render(game.window);

    settings.Render(game.window);
    close.Render(game.window);
}
 



as the picture is german, let me say that right to _Myptr and running it says <could not read memory>
the right table is the call stack.

the debug output says this:
Quote
Ausnahme (erste Chance) bei 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Schreiben an Position 0xFEEEFEEE
Ausnahmefehler bei 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Schreiben an Position 0xFEEEFEEE

Das Programm "[2032] Pew ReWork.exe" wurde mit Code 0 (0x0) beendet.

means something like: exception error at 0x50024EBE (sfml-window-2.dll) in Pew ReWork.exe: 0xC0000005: access violation when writing at position  0xFEEEFEEE
(freely translated, my english is a bit rusty)

thank you for your help

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #1 on: June 14, 2014, 11:48:00 pm »
The picture is broken. It says 'no hotlinking' on a fiery background.
Back to C++ gamedev with SFML in May 2023

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #2 on: June 14, 2014, 11:50:18 pm »
The picture is broken. It says 'no hotlinking' on a fiery background.

http://www.suckmypic.net/zgEByn4H.png <-link



one of there should work :D

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #3 on: June 14, 2014, 11:57:58 pm »
This is an educated guess: change it so that unique ptr to GameState is on a line BEFORE sf::RenderWindow in your game class, now it is after. Touch no other code. I'm sorry if I'm cryptic but I don't want to write elaborate explanation if it'll end up being wrong. Actually it's wrong already - I thought it's deletion order but you set the unique ptr to null yourself. :-[
« Last Edit: June 15, 2014, 12:05:17 am by FRex »
Back to C++ gamedev with SFML in May 2023

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #4 on: June 15, 2014, 12:05:19 am »
This is an educated guess: change it so that unique ptr to GameState is on a line BEFORE sf::RenderWindow in your game class, now it is after. Touch no other code. I'm sorry if I'm cryptic but I don't want to write elaborate explanation if it'll end up being wrong.

do you mean something like changing this:
class Game
{
public:
         Game();
        ~Game();

        enum class gameStates{ MAINMENU, INTRO, PLAY, SETTINGS, GRAPHICSET, SOUNDSET, DIFFSET, COOPSET, HIGHSCORE };

        void Run();
        void ChangeState(gameStates newState);
        bool isRunning(){ return running; };
        void setRunning(bool mRunning);
       
        sf::RenderWindow window;

private:

        void Quit();
        void Update();
        void HandleEvents();
        void Render();

        std::unique_ptr<GameState> CurrentState;
       
        bool running;
};
 

into this:
class Game
{
public:
         Game();
        ~Game();

        enum class gameStates{ MAINMENU, INTRO, PLAY, SETTINGS, GRAPHICSET, SOUNDSET, DIFFSET, COOPSET, HIGHSCORE };

        void Run();
        void ChangeState(gameStates newState);
        bool isRunning(){ return running; };
        void setRunning(bool mRunning);
       

        std::unique_ptr<GameState> CurrentState;
        sf::RenderWindow window;

private:

        void Quit();
        void Update();
        void HandleEvents();
        void Render();

        //here was the ptr
       
        bool running;
};
 

does not really change something :( it just makes my ptr public

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #5 on: June 15, 2014, 12:30:05 am »
When your MainMenuState calls ChangeState it deletes itself because of unique_ptr being set to something different and because of that things such as sf::Event, sf::Clock etc. that are its members are deleted already so in next pollEvent loop it'll crash, you should return immediately from HandleEvents if you called ChangeState.
Back to C++ gamedev with SFML in May 2023

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #6 on: June 15, 2014, 12:36:38 am »
When your MainMenuState calls ChangeState it deletes itself because of unique_ptr being set to something different and because of that things such as sf::Event, sf::Clock etc. that are its members are deleted already so in next pollEvent loop it'll crash, you should return immediately from HandleEvents if you called ChangeState.

i understand what you mean. but how can i realize this?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #7 on: June 15, 2014, 12:40:47 am »
You should make it so the unique ptr that was there originally is stored somewhere for 'delayed' deletion or return from HandleEvents if you called ChangeState.
Back to C++ gamedev with SFML in May 2023

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #8 on: June 15, 2014, 12:48:59 am »
You should make it so the unique ptr that was there originally is stored somewhere for 'delayed' deletion or return from HandleEvents if you called ChangeState.

mhh. okay i will think about how to handle that. but i find it strange, that this whole stuff works in this case:

//main.cpp
#include "Game.h"

int main()
{
        Game cyberPong;
        cyberPong.ChangeState(Game::gameStates::MAINMENU);

        while (cyberPong.isRunning())
        {
                cyberPong.Run();
        }

        return 0;
}
 

//GameState.h

#ifndef GAMESTATE_H
#define GAMESTATE_H

class Game;

class GameState
{
public:
        virtual void HandleEvents(Game &game) = 0;
        virtual void Update(Game &game) = 0;
        virtual void Draw(Game &game) = 0;
        virtual          ~GameState(){};
};

#endif
 

//Game.h

#ifndef GAME_H
#define GAME_H

#include <iostream>
#include <memory>
#include <SFML\Graphics.hpp>
#include "GameState.h"
#include "MainMenuState.h"
#include "PlayState.h"

class Game
{
public:
        Game();

        enum class gameStates{ MAINMENU, PLAY};

        void Run();
        void ChangeState(gameStates newState);
        bool isRunning();
        bool running;
        sf::RenderWindow window;
private:
        std::unique_ptr<GameState> CurrentState;
};

#endif
 

//Game.cpp

#include "Game.h"

Game::Game()
{
        window.create(sf::VideoMode(1280, 720), "CyberPong");
        window.setFramerateLimit(60);
        running = true;
}

void Game::Run()
{
        while (window.isOpen())
        {
                CurrentState->HandleEvents(*this);

                window.clear(sf::Color(0,134,194));
               
                CurrentState->Update(*this);
                CurrentState->Draw(*this);

                window.display();
        }
}

void Game::ChangeState(gameStates newState)
{
        switch (newState)
        {
                case gameStates::MAINMENU:
                        CurrentState = std::move(std::unique_ptr<MainMenuState>(new MainMenuState));
                        break;
                case gameStates::PLAY:
                        CurrentState = std::move(std::unique_ptr<PlayState>(new PlayState));
                        break;                 
        }
}

bool Game::isRunning()
{
        return running;
}
 

//MainMenuState.h

#ifndef MAINMENUSTATE_H
#define MAINMENUSTATE_H

#include "Game.h"

class MainMenuState : public GameState
{
public:
        MainMenuState();
        ~MainMenuState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Draw(Game &game);
private:
        bool bStartGameSelected;
        bool bQuitGameSelected;
        sf::Font font;
        sf::Text txtStartGame;
        sf::Text txtQuitGame;
};

#endif
 

//MainMenuState.cpp

#include "MainMenuState.h"

MainMenuState::MainMenuState()
{
        font.loadFromFile("Font//Saddlebag.ttf");

        txtStartGame.setFont(font);
        txtStartGame.setString("Spiel Starten");
        txtStartGame.setCharacterSize(30);
        txtStartGame.setPosition(400, 250);

        txtQuitGame.setFont(font);
        txtQuitGame.setString("Spiel Beenden");
        txtQuitGame.setCharacterSize(30);
        txtQuitGame.setPosition(400, 400);
}

void MainMenuState::HandleEvents(Game &game)
{
        sf::Event event;

        while (game.window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                {
                        game.window.close();
                        game.running = false;
                }
                if (event.type == sf::Event::MouseButtonReleased)
                {
                        if (event.mouseButton.button == 0)
                        {
                                if (bStartGameSelected)
                                        game.ChangeState(Game::gameStates::PLAY);
                                else if (bQuitGameSelected)
                                {
                                        game.window.close();
                                        game.running = false;
                                }
                        }
                }
        }
}

void MainMenuState::Update(Game &game)
{
        if (txtStartGame.getGlobalBounds().contains(
                sf::Mouse::getPosition(game.window).x,
                sf::Mouse::getPosition(game.window).y) &&
                txtStartGame.getColor() != sf::Color::Green)
        {
                txtStartGame.setColor(sf::Color::Green);
                bStartGameSelected = true;
        }
        else if (!txtStartGame.getGlobalBounds().contains(
                sf::Mouse::getPosition(game.window).x,
                sf::Mouse::getPosition(game.window).y) &&
                txtStartGame.getColor() == sf::Color::Green)
        {
                txtStartGame.setColor(sf::Color::White);
                bStartGameSelected = false;
        }

        if (txtQuitGame.getGlobalBounds().contains(
                sf::Mouse::getPosition(game.window).x,
                sf::Mouse::getPosition(game.window).y) &&
                txtQuitGame.getColor() != sf::Color::Green)
        {
                txtQuitGame.setColor(sf::Color::Green);
                bQuitGameSelected = true;
        }
        else if (!txtQuitGame.getGlobalBounds().contains(
                sf::Mouse::getPosition(game.window).x,
                sf::Mouse::getPosition(game.window).y) &&
                txtQuitGame.getColor() == sf::Color::Green)
        {
                txtQuitGame.setColor(sf::Color::White);
                bQuitGameSelected = false;
        }
}

void MainMenuState::Draw(Game &game)
{
        game.window.draw(txtStartGame);
        game.window.draw(txtQuitGame);
}

MainMenuState::~MainMenuState()
{
        std::cout << "Menudestruktor wurde aufgerufen" << std::endl;
}
 

//PlayState.h

#ifndef PLAYSTATE_H
#define PLAYSTATE_H

#include "Game.h"
#include "GameState.h"

class PlayState : public GameState
{
public:
        PlayState();
        ~PlayState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Draw(Game &game);
private:
        std::unique_ptr<sf::CircleShape> upBall;
        std::shared_ptr<sf::RectangleShape> spPad1;
        std::shared_ptr<sf::RectangleShape> spPad2;
};

#endif
 

//PlayState.cpp

#include "PlayState.h"

PlayState::PlayState()
{
        spPad1 = std::make_shared<sf::RectangleShape>(sf::Vector2f(53, 192));
        spPad2 = std::make_shared<sf::RectangleShape>(sf::Vector2f(53, 192));
        upBall = std::unique_ptr<sf::CircleShape>(new sf::CircleShape(20));

        spPad1->setPosition(0, 300);
        spPad2->setPosition(1227, 300);
}

PlayState::~PlayState()
{
        std::cout << "Playstate wurde zerstört" << std::endl;
}


void PlayState::HandleEvents(Game &game)
{
        sf::Event event;

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

void PlayState::Update(Game &game)
{

}

void PlayState::Draw(Game &game)
{
        game.window.draw(*upBall);
        game.window.draw(*spPad1);
        game.window.draw(*spPad2);
}
 

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #9 on: June 15, 2014, 12:52:52 am »
In these sf::Event is on stack as a local in function so it lasts as long as the scope it's in does not caring how long the class this function is called on lives, only member variables and this care about whether or not class lives. In MainMenuState you (for some reason..., that's really bad practice) allocate it with new in the class itself and that gets deleted in destructor when class gets freed by unique ptr.
Back to C++ gamedev with SFML in May 2023

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #10 on: June 15, 2014, 01:02:14 am »
In these sf::Event is on stack as a local in function so it lasts as long as the scope it's in does not caring how long the class this function is called on lives, only member variables and this care about whether or not class lives. In MainMenuState you (for some reason..., that's really bad practice) allocate it with new in the class itself and that gets deleted in destructor when class gets freed by unique ptr.

okay so i changed the mainMenuState.
it looks now like this:
//MainMenuState.h

#ifndef MAINMENUSTATE_H
#define MAINMENUSTATE_H

#include "Game.h"
#include "MenuSfx.h"
#include "IOstuff.h"

class MainMenuState : public GameState
{
public:
        MainMenuState();
        ~MainMenuState();
        void HandleEvents(Game &game);
        void Update(Game &game);
        void Render(Game &game);
private:
        IOsound iosound;
        MenuSound sound;

        bool  playing;
        float speed;
        float elapsedTime;
        float x_movement;
        float y_movement;
        float x, y;
        short int   debauch;
        short int   selection;
        int   volume;

        sf::Event   pEvent;
        sf::Clock   pClock;
        sf::Texture texture;
        sf::Sprite  sprite;

        Background bg;
        Text play, again, settings, close;
};
#endif
 

//MainMenuState.cpp

#define PI 3.14159
#include "MainMenuState.h"
#include <cmath>

MainMenuState::MainMenuState()
{
        bg.setFilePath("graphics//core//menu.png");
       
        //sound & music
        sound.LoadSoundBuffer();
        sound.setBuffer(volume);

        playing = false;
        speed = 0.5;
        selection = 0;
        iosound.ReadSoundSettings(volume);

        //Enemy
        elapsedTime = 0;
        x_movement = 0;
        y_movement = 0;
        debauch = 200;
        texture.loadFromFile("graphics//enemies//enemy.png");
        texture.setSmooth(false);
        sprite.setTexture(texture);
        sprite.setOrigin(36.5, 35);

        //buttons

        play.setStringAndSize("play", 70);
        again.setStringAndSize("again", 70);
        settings.setStringAndSize("settings", 70);
        close.setStringAndSize("close", 70);

        play.setPosition(270, 150);
        again.setPosition(270, 150);
        settings.setPosition(270, 250);
        close.setPosition(270, 350);
}

MainMenuState::~MainMenuState()
{
        std::cout << "zerstoert aldaa";
}

void MainMenuState::HandleEvents(Game &game)
{
        while (game.window.pollEvent(pEvent))
        {
                if (pEvent.type == sf::Event::Closed)
                        game.setRunning(false);

                //Keyboard selection
                if (pEvent.type == sf::Event::KeyPressed)
                {
                        switch (pEvent.key.code)
                        {
                        case sf::Keyboard::Up:
                                if (selection > 0)
                                {
                                        selection -= 1;
                                        sound.PlaySound("select");
                                }
                                else
                                        selection = 0;
                                break;

                        case sf::Keyboard::Down:
                                if (selection < 2)
                                {
                                        selection += 1;
                                        sound.PlaySound("select");
                                }
                                else
                                        selection = 2;
                                break;

                        case sf::Keyboard::Return:
                                if (selection == 0)
                                {
                                        if (playing)
                                                game.ChangeState(Game::gameStates::PLAY);
                                        else
                                                playing = true;
                                                game.ChangeState(Game::gameStates::INTRO);
                                }
                                else if (selection == 1)
                                        game.ChangeState(Game::gameStates::SETTINGS);
                                else
                                        game.setRunning(false);
                                break;
                        default:
                                break;
                        }
                }
        }
}

void MainMenuState::Update(Game &game)
{
        if (selection == 0)//Spielen
        {
                play.setColor(sf::Color(255, 128, 0));
                again.setColor(sf::Color(255, 128, 0));
                settings.setColor(sf::Color(255, 255, 255));
                close.setColor(sf::Color(255, 255, 255));
        }
        else if (selection == 1)//Settings
        {
                play.setColor(sf::Color(255, 255, 255));
                again.setColor(sf::Color(255, 255, 255));
                settings.setColor(sf::Color(255, 128, 0));
                close.setColor(sf::Color(255, 255, 255));
        }
        else//Beenden
        {
                play.setColor(sf::Color(255, 255, 255));
                again.setColor(sf::Color(255, 255, 255));
                settings.setColor(sf::Color(255, 255, 255));
                close.setColor(sf::Color(255, 128, 0));
        }

        //moving enemy
        elapsedTime = pClock.restart().asMilliseconds();
        x_movement += elapsedTime;
        y_movement += elapsedTime;
        y = sprite.getPosition().y;
        x = sprite.getPosition().x;
        y = 300 + std::sin((y_movement * PI) / 180) * debauch;
        x = 400 + std::cos((x_movement * PI) / 180) * debauch;

        if (x_movement > 360)
                x_movement = 0;

        if (y_movement > 360)
                y_movement = 0;

        sprite.setPosition(x, y);
}

void MainMenuState::Render(Game &game)
{
        bg.Render(game.window);
        game.window.draw(sprite);

        if (playing)
                again.Render(game.window);
        else
                play.Render(game.window);

        settings.Render(game.window);
        close.Render(game.window);
}
 
i can now switch the state.

but when the state is changed i really quick get the next error:

Quote
HEAP[Pew ReWork.exe]: HEAP: Free Heap block 55f87d8 modified at 55f8854 after it was freed
Pew ReWork.exe hat einen Haltepunkt ausgelöst.

Das Programm "[1560] Pew ReWork.exe" wurde mit Code 0 (0x0) beendet.

http://www.suckmypic.net/C38mbus8.png



well :D what da f*** :'D

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #11 on: June 15, 2014, 01:07:50 am »
as this happens in the intro state, i think it would be good to see that code too... too much? :D

//Intro.h

#ifndef INTRO_H
#define INTRO_H

#include "Game.h"


class Intro : public GameState
{
public:
        Intro();
        ~Intro();

        void HandleEvents(Game &game);
        void Update(Game &game);
        void Render(Game &game);

private:
        Text startText;

        sf::Event   pEvent;
        sf::Clock   pClock;
        sf::Texture intro;
        sf::Sprite  introSprite;
        float elapsedTime;
        float bgSpeed, y;
        bool startintro;
        int returnCounter;
};

#endif
 

//Intro.cpp

#include "Intro.h"

Intro::Intro()
{
        //basic
        elapsedTime = 0;
        startintro = false;
        returnCounter = 0;

        //background
        startText.setStringAndSize("Return to start", 60);

        intro.loadFromFile("graphics//core//intro.jpg");
        intro.setSmooth(false);
        introSprite.setTexture(intro);
        introSprite.setPosition(0, 600);
        bgSpeed = 0.03;
        y = introSprite.getPosition().y;
}

Intro::~Intro()
{
}

void Intro::HandleEvents(Game &game)
{
        while (game.window.pollEvent(pEvent))
        {
                if (pEvent.type == sf::Event::Closed)
                        game.setRunning(false);

                if (pEvent.type == sf::Event::KeyPressed)
                {
                        if (pEvent.key.code == sf::Keyboard::Return)
                                returnCounter += 1;

                        if (pEvent.key.code == sf::Keyboard::Escape)
                                game.setRunning(false);
                }
        }
}
void Intro::Update(Game &game)
{
        if (!startintro)
        {
                startText.setOrigin(startText.getGlobalBounds().width / 2, startText.getGlobalBounds().height / 2);
                startText.setPosition(game.window.getSize().x / 2, game.window.getSize().y / 2);
                introSprite.setPosition(0, 600);
        }

        if (returnCounter == 1)
                startintro = true;

        if (returnCounter >= 2)
                game.ChangeState(Game::gameStates::PLAY);

        //move background
        elapsedTime = pClock.restart().asMilliseconds();
        y -= bgSpeed * elapsedTime;
        introSprite.setPosition(0, y);

        //end intro
        if (y <= -1200)
        {
                game.ChangeState(Game::gameStates::PLAY);
                startintro = false;
        }
}
void Intro::Render(Game &game)
{
        if (!startintro)
                startText.Render(game.window);

        if (startintro)
                game.window.draw(introSprite);
}
 

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #12 on: June 15, 2014, 01:10:16 am »
It's STILL a member variable so you can't use it after you call ChangeState, this error is quite clear on that - you modified it after it was freed. This is different that the other, working code where sf::Event was local to function.
In last one the error/crash was more cryptic, but the fact it was in window.dll (only pollEvent and a handlful of other functions are from window.dll, the drawing ones are all from graphics.dll) and 0xFEEEFEEE being present was hinting at using a (sf::Event) pointer after it was freed, since that's what CRT on Windows is setting freed heap memory to.
Back to C++ gamedev with SFML in May 2023

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #13 on: June 15, 2014, 01:15:39 am »
thank you ;) i tested some things, it must be as you say. i got something wrong in my mainmenu class. i will try to fix this tomorrow :D

Edit:

Sorry, but i just can't find whats wrong...
« Last Edit: June 15, 2014, 04:47:22 pm by nebula »

nebula

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
    • Email
Re: access violation with unique pointer (c++)
« Reply #14 on: June 19, 2014, 04:51:48 pm »
okay, i got it running :P

the mistake was in the intro

changed it to this:
//Intro.h

#ifndef INTRO_H
#define INTRO_H

#include "Game.h"


class Intro : public GameState
{
public:
        Intro();
        ~Intro();

        void HandleEvents(Game &game);
        void Update(Game &game);
        void Render(Game &game);

private:
        sf::Clock   pClock;
        sf::Texture intro;
        sf::Sprite  introSprite;
        float elapsedTime;
        float bgSpeed, y;
};

#endif
 

//Intro.cpp

#include "Intro.h"

Intro::Intro()
{
        elapsedTime = 0;

        intro.loadFromFile("graphics//core//intro.jpg");
        intro.setSmooth(false);
        introSprite.setTexture(intro);
        introSprite.setPosition(0, 600);
        bgSpeed = 0.03;
        y = introSprite.getPosition().y;
}

Intro::~Intro()
{
}

void Intro::HandleEvents(Game &game)
{
        sf::Event pEvent;

        while (game.window.pollEvent(pEvent))
        {
                if (pEvent.type == sf::Event::Closed)
                        game.setRunning(false);

                if (pEvent.type == sf::Event::KeyPressed)
                {
                        if (pEvent.key.code == sf::Keyboard::Escape)
                                game.setRunning(false);

                        if (pEvent.key.code == sf::Keyboard::Return)
                                game.ChangeState(Game::gameStates::PLAY);
                }
        }
}
void Intro::Update(Game &game)
{
        elapsedTime = pClock.restart().asMilliseconds();
        y -= bgSpeed * elapsedTime;
        introSprite.setPosition(0, y);

        if (y <= -1200)
                game.ChangeState(Game::gameStates::PLAY);
}
void Intro::Render(Game &game)
{
                game.window.draw(introSprite);
}
 

thanks for the help ;) this topic can now be closed

 

anything