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

Author Topic: (Solved) Read access violation with unknown reason  (Read 1522 times)

0 Members and 1 Guest are viewing this topic.

EldarFara

  • Newbie
  • *
  • Posts: 6
    • View Profile
(Solved) Read access violation with unknown reason
« on: April 18, 2020, 01:41:20 am »
Hello! I am getting read access violation while running game code, here is a minimal example:
https://pastebin.com/cEqKDaGk
It is designed to render some sprites to the RenderTexture and then render it to the window.
Exception occures on any line of Menu::DrawMenu().
(click to show/hide)
Code seems to work OK if I am not using vectors for storing Menu class.
C++ 17, SFML 2.5.1, VS 2019
« Last Edit: April 18, 2020, 12:40:39 pm by EldarFara »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Read access violation with unknown reason
« Reply #1 on: April 18, 2020, 09:52:23 am »
Quote
Menus.push_back(&Menu());
You're storing the address of a temporary object. It is destroyed immediately so the address points to garbage after this line.
Laurent Gomila - SFML developer

EldarFara

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Read access violation with unknown reason
« Reply #2 on: April 18, 2020, 12:33:36 pm »
Thanks for your answer. I changed vector of object pointers to vector of objects (changed RenderTexture to RenderTexture* to make copy assignment possible) and now main() looks like this:
(click to show/hide)

This time, I get read access violation "this was 0x30" on the line with drawing to the window.

EDIT: I simplified code so much (to show a minimal example) that I deleted part with creating window, thats why I got read access violation. Problem is solved now. Cheers!
« Last Edit: April 18, 2020, 12:41:16 pm by EldarFara »