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

Author Topic: [SOLVED]SFML Access violation  (Read 4212 times)

0 Members and 1 Guest are viewing this topic.

trash_empire

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
[SOLVED]SFML Access violation
« on: August 24, 2015, 09:57:13 pm »
I am using SFML to create a simple tower defense game. I got it working, and now I want to get into some more complicated game logic. So I wanted to organize the code a bit before doing that. I was running most everything out of the main with only 2 other classes. So to organize I decided to make a game class.

When I was using the main, I was loading textures just fine. This is how I did it in the main.
//main
sf::Texture* texture1 = new sf::Texture;
texture1 ->loadFromFile("filelocation");
 
After I did this, and dereferenced it when using it, it was working fine.

However, when I created a class with
sf::Texture* texture1;
as a public member variable, and initializing it in the constructor like this, it doesn't work.

//Game constructor.
texture1 = new sf::Texture;
texture1->loadFromFile("filelocation");
 

I've also tried using
texture1->create(50,50);
but that didn't do anything.

The error I am getting is
0xC0000005: Access violation writing location 0x0000000000000008.

I had this same error when I was incorrectly using pointers in the main, but now I don't know what is doing wrong.

The error means that it is trying to access a null pointer, but I declared the pointer with the Texture constructor, so I don't know what the problem is.

I am calling the Game constructor in the main like this:

//main
Game game = Game();
 

Any help would be greatly appreciated, Thanks! :) :) :)
« Last Edit: August 24, 2015, 11:50:20 pm by trash_empire »

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: SFML Access violation
« Reply #1 on: August 24, 2015, 10:21:42 pm »
Try this code:
texture1 = new sf::Texture();

And in main:
Game game;

Also you should avoid using raw pointers whenever possible. If you absolutely need to use them then take a look at the smart pointers.

trash_empire

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: SFML Access violation
« Reply #2 on: August 24, 2015, 10:24:37 pm »
Try this code:
texture1 = new sf::Texture();

And in main:
Game game;

Also you should avoid using raw pointers whenever possible. If you absolutely need to use them then take a look at the smart pointers.
I tried that, I still get the same error.

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: SFML Access violation
« Reply #3 on: August 24, 2015, 10:56:56 pm »
I can't really tell what's the actual problem without being able to see more of the code...

trash_empire

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: SFML Access violation
« Reply #4 on: August 24, 2015, 10:58:32 pm »
This is pretty much all of the code. Everything was working before I tried to load textures. I don't do anything with the textures yet, I just want to load them. The rest of the code is arbritrary.

trash_empire

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: [SOLVED]SFML Access violation
« Reply #5 on: August 24, 2015, 11:52:51 pm »
The fix is actually quite strange, and I don't much understand it.

The way I was doing it was correct, however, at the top of my Game.cpp class I thought I had to declare variables there to be used within the class from the .h file. One of those was sf::RenderWindow window;

After deleting that, it worked fine. I guess you can not double declare a sf::RenderWindow object of the same name.

This is wierd, because I have double declared variables in other files before with no problem. I guess I shouldn't.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: [SOLVED]SFML Access violation
« Reply #6 on: August 25, 2015, 12:59:45 am »
All member variables are only declared once, in the class declaration (header file), unless they are static members. In which case, they must also be defined (also initialized here too) (source file).

If this double declaring thing has worked for you before, it's mostly due to luck, and not using more than one instance of a class, would be my guess.

trash_empire

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: [SOLVED]SFML Access violation
« Reply #7 on: August 25, 2015, 02:11:01 am »
All member variables are only declared once, in the class declaration (header file), unless they are static members. In which case, they must also be defined (also initialized here too) (source file).

If this double declaring thing has worked for you before, it's mostly due to luck, and not using more than one instance of a class, would be my guess.
Yeah, I realize now that it was because I wasn't preceding my functions with the class name and two colons.
I had tried that, then realized that I forgot to fix the colon thing, then I would fix it and the double declaration would still be there.

 

anything