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

Author Topic: Im getting exception thrown "Access violation reading location"  (Read 2316 times)

0 Members and 1 Guest are viewing this topic.

Azmuth

  • Newbie
  • *
  • Posts: 2
    • View Profile
Im getting exception thrown "Access violation reading location"
« on: September 26, 2023, 11:40:14 pm »
Hello, I have created a project in Visual Studio 2019, configured SFML, confirmed it was working with the example drawing of a simple texture. Right now, I am getting an error:
"Exception thrown at 0x00007FFC0A282B19 (sfml-graphics-d-2.dll) in sfml-app.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF. occurred" and I do not understand why. I have attached my code - the part that causes this in main.cpp - lines 47-52, 57-58. Seems like it is somehow connected to the vector, but I can't figure out the cause.

Please help me!

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Im getting exception thrown "Access violation reading location"
« Reply #1 on: September 27, 2023, 01:47:54 am »
I haven't tried running the code, but looking over it I'd guess the issue is how you are storing fonts.
When you set the font of a Text object, it stores it as a Font pointer. You load and store the font as part of your MenuBtn class.
Pushing temporary MenuBtn objects into a vector will result in the MenuBtn moving around in memory. But that breaks the Font pointer.

You'll need to make sure the Font doesn't move around.

(Also, it's not part of the crash, but loading a copy of a font for each button is inefficient. You only need one copy of the font then tell all Text objects to use the same one)

Azmuth

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Im getting exception thrown "Access violation reading location"
« Reply #2 on: September 27, 2023, 04:00:53 pm »
Thank you so much, your solution did resolve this problem. I totally agree it was very inefficient. Now I only have one copy of Font object in main and pass it by reference to the MenuBtn objects.