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

Author Topic: Handleing First Chance Error when loading images  (Read 1917 times)

0 Members and 3 Guests are viewing this topic.

TheDianamu

  • Newbie
  • *
  • Posts: 20
    • View Profile
Handleing First Chance Error when loading images
« on: November 02, 2013, 08:11:42 am »
Hi, whenever I run my application, and I want to set rect image to the grass. It keeps on giving me a first chance exception. Here is the 2 stacktraces

First-chance exception at 0x1004ab8d in Game.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x1004ab8d in Game.exe: 0xC0000005: Access violation writing location 0x00000000.

I'm a little confused, could anybody tell me what is going on? I have added the code that is drawing and setting the rect on the screen below.

sf::RectangleShape rect;

sf::Texture* grass;
grass->loadFromFile("grass.png");

rect.setSize(sf::Vector2f(16,16));
rect.setTexture(grass);
rect.setPosition(x * 16, y * 16);
window.draw(rect);

window.setView(map_view);
window.display();

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Handleing First Chance Error when loading images
« Reply #1 on: November 02, 2013, 08:15:49 am »
Your grass pointer is not the address of a valid sf::Texture object, its value is undefined since you don't initialize it.

Don't use pointers if you don't know how to use them. You don't even need them here.
Laurent Gomila - SFML developer

TheDianamu

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Handleing First Chance Error when loading images
« Reply #2 on: November 02, 2013, 08:18:19 am »
I know how to use them a bit. But I have put it there because I get the following error once I debug without the pointer.

d:\game\lengine.cpp(55): error C2664: 'sf::Shape::setTexture' : cannot convert parameter 1 from 'sf::Texture' to 'const sf::Texture *'
          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Handleing First Chance Error when loading images
« Reply #3 on: November 02, 2013, 09:23:16 am »
Quote
I know how to use them a bit.
Not enough, apparently.

Quote
But I have put it there because I get the following error once I debug without the pointer.
You need to pass the address of the texture to the function.

rect.setTexture(&grass);
Laurent Gomila - SFML developer

 

anything