By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

Need help with a 'Read access violation' exception in an SFML/C++ project:

P: 6
I'm recently learning to make a 2d game in SFML using a tutorial series on youtube by Suraj Sharma(Video 57):

https://www.youtube.com/watch?v=kwd_AVCkvXE&list=PL6xSOsbVA1ebkU66okpi-KViAO8_9DJKg&index=57

His Source Code:

https://github.com/Headturna/SFML_RPG

Right now he's teaching me to symplify the game's menu system by making a mini class 'StateData' in the parent class 'State' so any inherited class can access 'State' parameters via 'StateData'(Ex:MainMenu(StData* Stdata){}).

After debugging the game seems to runs fine.But everytime i click on something on the menu(start,settings,etc...) a 'Read access violation:Stdata was nullptr' occurs in the 'State' class constructor.

Here's the code:

State.h:
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2.  
  3. #ifndef STATE_H
  4. #define STATE_H
  5.  
  6. #include "Player.h"
  7. #include "GrphSettings.h"
  8.  
  9. class Player;
  10. class GrphSettings;
  11. class State;
  12.  
  13. class StData {
  14. public:
  15.     StData(){}
  16.  
  17.     //Vars
  18.     float GridSize;
  19.     sf::RenderWindow* Window;
  20.     GrphSettings* GSettings;
  21.     std::map<std::string, int>* SupportedKeys;
  22.     std::stack<State*>* states;
  23. };
  24.  
  25. class State
  26. {
  27. private:
  28.  
  29. protected:
  30.     StData* Stdata;
  31.     std::stack<State*>* states;
  32.     sf::RenderWindow* window;
  33.     std::map<std::string, int>* SupportedKeys ;
  34.     std::map<std::string, int> Keybinds;
  35.     bool quit;
  36.     bool pause;
  37.     float keyTime; 
  38.     float keyTimeMax;
  39.     float GridSize;
  40.  
  41.     sf::Vector2i MousePosScr;
  42.     sf::Vector2i MousePosWind;
  43.     sf::Vector2f MousePosView;
  44.     //Resources
  45.     std::map<std::string,sf::Texture> texture;
  46.     //Funcs
  47.     virtual void InitKeybinds() = 0;
  48. public:
  49.     State(StData* Stdata);
  50.     virtual~State();
  51.     //Access
  52.     const bool getKeytime();
  53.     const bool& getquit()const;
  54.     //Funcs
  55.     void Endstate();
  56.     void PauseSt();
  57.     void UnPauseSt();
  58.     virtual void UpdateInput(const float& dt) = 0;
  59.     virtual void UpdateMousePos();
  60.     virtual void UpdateKeyTime(const float& dt);
  61.     virtual void Update(const float& dt) = 0;
  62.     virtual void Render(sf::RenderTarget* target = nullptr) = 0;
  63. };
  64. #endif // !1
State.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "pch.h"
  2. #include "State.h"
  3.  
  4. State::State(StData* Stdata)
  5.     this->Stdata = Stdata;
  6.     this->window = Stdata->Window;//Read access violation
  7.     this->SupportedKeys = Stdata->SupportedKeys;
  8.     this->states = Stdata->states;
  9.     this->quit = false;
  10.     this->pause = false;
  11.     this->keyTime = 0.f;
  12.     this->keyTimeMax = 10.f;
  13.     this->GridSize = Stdata->GridSize;
  14. }
  15.  
  16. State::~State()
  17. {
  18. }
  19. //Access
  20. const bool State::getKeytime()
  21. {
  22.     if (this->keyTime >= this->keyTimeMax) {
  23.         this->keyTime = 0.f; 
  24.         return true;
  25.     }
  26.     return false;
  27. }
  28.  
  29. const bool& State::getquit() const
  30. {
  31.     // TODO: insert return statement here
  32.     return this->quit;
  33. }
  34. //Funcs
  35. void State::Endstate()
  36. {
  37.     this->quit = true;
  38. }
  39.  
  40. void State::PauseSt()
  41. {
  42.     this->pause = true;
  43. }
  44.  
  45. void State::UnPauseSt()
  46. {
  47.     this->pause = false;
  48. }
  49.  
  50. void State::UpdateMousePos()
  51. {
  52.     this->MousePosScr = sf::Mouse::getPosition();
  53.     this->MousePosWind = sf::Mouse::getPosition(*this->window);
  54.     this->MousePosView = this->window->mapPixelToCoords(sf::Mouse::getPosition(*this->window));
  55. }
  56.  
  57. void State::UpdateKeyTime(const float& dt)
  58. {
  59.     if (this->keyTime < this->keyTimeMax)
  60.         this->keyTime += 100.f * dt;
  61. }
  62.  
Every button one the menu(start,exit...)is an inherited state from 'State' class.This is,for example,the 'Edit' state.

Edit.h:
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #ifndef EDIT_H
  3. #define EDIT_H
  4.  
  5. #include "State.h"
  6. #include "Gui.h"
  7. #include "PauseMenu.h"
  8. #include "TileMap.h"
  9.  
  10. class State;
  11. class Gui;
  12. class PauseMenu;
  13. class TileMap;
  14.  
  15. class Edit:public State
  16. {
  17. private:
  18.     //Vars
  19.     sf::Font Fnt;
  20.     PauseMenu* PMenu;
  21.     std::map<std::string, gui::Button*> buttons;
  22.     TileMap Map;
  23.  
  24.     //Functions
  25.     void InitVars();
  26.     void InitBackGrnd();
  27.     void InitFonts();
  28.     void InitKeybinds();
  29.     void InitPauseMenu();
  30.     void InitBtn();
  31. public:
  32.     Edit(StData* Stdata);
  33.     virtual~Edit();
  34.     //Functions
  35.     void UpdateInput(const float& dt);
  36.     void Update(const float& dt);
  37.     void UpdatePButtons();
  38.     void UpdateBtn();
  39.  
  40.     void Endstate();
  41.  
  42.     void RenderBtn(sf::RenderTarget& target);
  43.     void Render(sf::RenderTarget* target = nullptr);
  44. };
  45. #endif // ! EDIT_H
Edit.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "pch.h"
  2. #include "Edit.h"
  3.  
  4. void Edit::InitVars()
  5. {
  6. }
  7.  
  8. void Edit::InitBackGrnd()
  9. {
  10. }
  11.  
  12. void Edit::InitFonts()
  13. {
  14.     if (!this->Fnt.loadFromFile("Fonts/SPACEMAN.ttf")) {
  15.         throw("Error::Edit::Couldn't load font");
  16.     }
  17. }
  18.  
  19. void Edit::InitKeybinds()
  20. {
  21.     std::ifstream ifs("Config/EditKeys.ini");
  22.  
  23.     if (ifs.is_open()) {
  24.         std::string key = "";
  25.         std::string key2 = "";
  26.         int keyval = 0;
  27.         while (ifs >> key >> key2)
  28.         {
  29.             this->Keybinds[key] = this->SupportedKeys->at(key2);
  30.         }
  31.     }
  32.     ifs.close();
  33.  
  34.     this->Keybinds["Close"] = this->SupportedKeys->at("ESC");
  35.     this->Keybinds["Left"] = this->SupportedKeys->at("A");
  36.     this->Keybinds["Right"] = this->SupportedKeys->at("D");
  37.     this->Keybinds["Up"] = this->SupportedKeys->at("W");
  38.     this->Keybinds["Down"] = this->SupportedKeys->at("S");
  39. }
  40.  
  41. void Edit::InitPauseMenu()
  42. {
  43.     this->PMenu = new PauseMenu(*this->window, this->Fnt);
  44.     this->PMenu->addButtons("Quit", 800.f, "Quit");
  45. }
  46.  
  47. void Edit::InitBtn()
  48. {
  49. }
  50.  
  51. Edit::Edit(StData* Stdata)
  52.     :State(Stdata)
  53. {
  54.     this->InitVars();
  55.     this->InitBackGrnd();
  56.     this->InitFonts();
  57.     this->InitKeybinds();
  58.     this->InitPauseMenu();
  59.     this->InitBtn();
  60. }
  61.  
  62. Edit::~Edit()
  63. {
  64.     auto it = this->buttons.begin();
  65.     for (it = this->buttons.begin(); it != this->buttons.end(); ++it) {
  66.         delete it->second;
  67.     }
  68.     delete this->PMenu;
  69. }
  70.  
  71. //Funcs
  72. void Edit::UpdateInput(const float& dt)
  73. {
  74.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(this->Keybinds.at("Close")))
  75.         && this->getKeytime()) {
  76.         if (!this->pause)
  77.             this->PauseSt();
  78.         else this->UnPauseSt();
  79.     }
  80.  
  81. }
  82.  
  83. void Edit::Update(const float& dt)
  84. {
  85.     this->UpdateMousePos();
  86.     this->UpdateKeyTime(dt);
  87.     this->UpdateInput(dt);
  88.  
  89.     if (!this->pause) {//Unpaused
  90.         this->UpdateBtn();
  91.     }
  92.     else {//Paused
  93.         this->PMenu->Update(this->MousePosView);
  94.         this->UpdatePButtons();
  95.     }
  96.  
  97.     this->UpdateBtn();
  98.  
  99.     std::cout << this->MousePosView.x << " " << this->MousePosView.y << "\r";
  100. }
  101.  
  102. void Edit::UpdatePButtons()
  103. {
  104.     if (this->PMenu->isPressed("Quit"))
  105.             this->Endstate();
  106. }
  107.  
  108. void Edit::UpdateBtn()
  109. {    //Update buttons and handle their functions
  110.     for (auto& it : this->buttons) {
  111.         it.second->Update(MousePosView);
  112.     }
  113. }
  114.  
  115. void Edit::Endstate()
  116. {
  117.     std::cout << "Ending State" << "\n";
  118. }
  119.  
  120. void Edit::RenderBtn(sf::RenderTarget& target)
  121. {
  122.     for (auto& it : this->buttons) {
  123.         it.second->Render(target);
  124.     }
  125. }
  126.  
  127. void Edit::Render(sf::RenderTarget* target)
  128. {
  129.     if (!target)
  130.         target = this->window;
  131.  
  132.     this->RenderBtn(*target);
  133.  
  134.     this->Map.Render(*target);
  135.  
  136.     if (this->pause) {//Pause Menu
  137.         this->PMenu->Render(*target);
  138.     }
  139.  
  140.     sf::Text MText;
  141.     MText.setPosition(this->MousePosView);
  142.     MText.setFont(this->Fnt);
  143.     MText.setCharacterSize(12);
  144.     std::stringstream ss;
  145.     ss << this->MousePosView.x << ' ' << this->MousePosView.y;
  146.     MText.setString(ss.str());
  147.  
  148.     target->draw(MText);
  149. }
  150.  
Can anyone help me ?
3 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


P: 6
One more information,it appears that in the 'State.h' file the 'StData()' constructor are having warning 'C26495' complaining that the following variables 'Windows' , 'GSettings' , 'SupportedKeys' , 'states' , 'GridSize' are uninitialized.That didn't happen in the tutorial.
3 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.