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

Author Topic: Access violation reading location  (Read 10895 times)

0 Members and 1 Guest are viewing this topic.

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Access violation reading location
« on: January 13, 2019, 02:01:24 am »
I found a few topics with this error but none really looked similarly to my issue so here I go.
I have a class that has a vector of sf::Text. Then it has functions to set everything up like fonts sizes and coordinates so thats fine. Next I'd like to draw the text from main, as it's private in a class I tried to do it from the classes function like that:
void Player::drawCards(sf::RenderWindow &window)
{
        for(int i = 0; i<3; i++)
        {
        window.draw(myclues[i]);
        }
}
 
That didn't work out, so I made a function that returns the text.
sf::Text Player::retClu(int i)
{
        return myclues[i];
}
 
And then use it in main.cpp like so (the class - player, is also in a vector so I can do this for each generated class) :
for (int i = 0; i < numb_of_players; i++)
                {
                        for (int j = 0; j < 3; j++)
                        {
                                window.draw(vPlayers[i].retClu(j));  
                        }
                }
 
And yet again I get the error:
Exception thrown at 0x508D485F (sfml-graphics-d-2.dll) in forensic_15.exe: 0xC0000005: Access violation reading location 0x00000004.
 
I've tried many different ways of coding this but I must have some misunderstanding of how it can/should be done because I get this error every time.
Thanks for any replies.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Access violation reading location
« Reply #1 on: January 13, 2019, 05:28:48 am »
Some NULL pointer somewhere? That error looks like if something tried to read starting at 5th byte via NULL pointer and it ended up with 0x00000004 but that can be deceptive.

Can you make a self-contained example? Are you sure you're not mixing different versions, compilers, debug and release, etc.?
Back to C++ gamedev with SFML in May 2023

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Access violation reading location
« Reply #2 on: January 13, 2019, 12:27:06 pm »
I dont think anything in the setup is wrong, before it was compiling and everything was fine, just this one thing gives me an error.
So is it a valid way to do this? I started to think that it just cannot be done this way.

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Access violation reading location
« Reply #3 on: January 13, 2019, 12:37:13 pm »
I've read somewhere that maybe the font isn't ketp alive? Not sure what that means, the font and everything for the sf::Text is created and added in another of the classes function, looks like this:
void Player::setTextLoc(int numb)
{
        sf::Font font;
        if (!font.loadFromFile("28_Days_Later.ttf")) { std::cerr << "ERROR loading 28_Days_Later.ttf" << std::endl; }
       
        float pos_ay = 300.f;
       
        for (auto& a : myclues)
        {
                a.setFont(font);
                a.setPosition(30.f , pos_ay);
                a.setCharacterSize(20);
                a.setFillColor(sf::Color(0, 0, 0, 255));
                pos_ay += 10.f;        
        }
}
 

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Access violation reading location
« Reply #4 on: January 13, 2019, 12:39:48 pm »
Yes, that's it.
Back to C++ gamedev with SFML in May 2023

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Access violation reading location
« Reply #5 on: January 13, 2019, 12:56:42 pm »
It actually didn't change anything  :'( :'(
I changed my code to that:
void Player::setTextLoc(int numb, sf::Font font)
{
        float pos_ay = 300.f;
        int n = numb;
       
        for (auto& a : myclues)
        {
                a.setFont(font);
                a.setPosition(30.f , pos_ay);
                a.setCharacterSize(20);
                a.setFillColor(sf::Color(0, 0, 0, 255));
                pos_ay += 10.f;        
        }
}
 
And then made sf::Font in main and called the function:
sf::Font font;
        if (!font.loadFromFile("28_Days_Later.ttf")) { std::cerr << "ERROR loading font from file" << std::endl; }
        for (auto & a : vPlayers)
        {
                a.setTextLoc(numb, font);
        }
 
But it throws the exception just the same.  :'(

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Access violation reading location
« Reply #6 on: January 13, 2019, 12:57:51 pm »
Font in setTextLoc is a copy that's local to that function still. You should pass a reference to it instead (const sf::Font& font instead of sf::Font font).
Back to C++ gamedev with SFML in May 2023

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Access violation reading location
« Reply #7 on: January 13, 2019, 01:02:07 pm »
Oh my God, obviously. I wanted to try that out so fast that I completely missed that. Thanks so much!!!
I still dont understand how that works ("keeping the font alive" or what issue does it have with having just a copy not an original), but now everything is fine. Thanks again!

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Access violation reading location
« Reply #8 on: January 13, 2019, 01:03:47 pm »
One last question, it started woking when I added &. Why make it a constant, does that change things much? I never actually use it tbh.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Access violation reading location
« Reply #9 on: January 13, 2019, 01:10:18 pm »
The 'keeping alive thing' is basic C++ thing.
Locals get their destructors called at the end of their scope.
It's good programmer practice to put const whenever you don't intend to change something, when using references and pointers especially.
Back to C++ gamedev with SFML in May 2023