C++ SFML Array Error: Access violation reading location 0xC0000005

1

I am making a game with C++ and SFML, and I am having a problem during rendering various items with array. When I try to draw an array of sprites, the debugging is alright, and there are no warning and errors, but my game program runs only 20 seconds, then it stops running, and says, 'Unhandled exception at 0x7C50CF2E(sfml-graphics-d-2.dll), ECOVID-19 SFML.exe): 0xC0000005: Access violation reading location 0xCCCCCCD0.' I did everything I can, but I don't know why this exception occurs. Here are my codes that I suspect the causation of the error. Thank you for reading in spite of my poor English.

 #include <SFML/Graphics.hpp>
 ...
 using namespace std;
 using namespace sf;
 ...

 int main () {
 ...

 //item Sprites

 Texture bombTex;
 bombTex.loadFromFile("images/bomb.png");
 Sprite bomb;
 ...
 Texture bomb2Tex;
 bomb2Tex.loadFromFile("images/bomb_2.png");
 Sprite bomb_2;
 ...
 Texture cakeTex;
 cakeTex.loadFromFile("images/cake.png");
 Sprite cake;
 ...
 Texture coffeeTex;
 coffeeTex.loadFromFile("images/coffee.png");
 Sprite coffee;
 ...
 Texture chickenTex;
 chickenTex.loadFromFile("images/chicken.png");
 Sprite chicken;
 ...
 Texture pizzaTex;
 pizzaTex.loadFromFile("images/pizza.png");
 Sprite pizza;

 //item array (I made an item array to display & render various items in the game screen.)
 Sprite item[10]; //Although I change the array size to 4 or 5, the same exception occurs.
 item[0] = bomb;
 item[1] = coffee;
 item[2] = bomb_2;
 item[3] = chicken;
 item[4] = pizza;

 std::vector<Sprite> items;
 items.push_back(Sprite(item[4]));

 ...

 while (window.isOpen())
 {   ...
     ...
  for (size_t i = 0; i < items.size(); i++)
  {
     if (humanArr[index].getGlobalBounds().intersects(item[i].getGlobalBounds())) 
     //humanArr[index] is a player Sprite.
      {
         ...
          items.erase(items.begin() + i);
       }
   }
   ...
  window.clear();
   ...
  for (size_t i = 0; i < items.size(); i++)
     {
         window.draw(item[i]); // <- the exception error occurs here.
      }

   ...
 window.display();
  }
 return 0;
}
c++
sfml
asked on Stack Overflow Aug 20, 2020 by Angelina

1 Answer

0

What may be happening is that when you copy the Sprite item[10]; to the std::vector<Sprite> items; the Sprite class is making a shallow copy. It means that if the Sprite class is allocating any memory with new operator and storing it in a member pointer, then the shallow copy will only copy the address to which the pointer is pointing. When you do call items.erase(items.begin() + i); the destructor of the Sprite will be called, and in the destructor it may be calling delete in that pointer to some resourse.

When you call window.draw(item[i]); the library will try to use that resource and will find an invalid address.

What I suggest is that you don't use the Sprite item[10]; and only the std::vector<Sprite> items; , like this:

std::vector<Sprite> items;

items.push_back(bomb);
items.push_back(coffee);
...
window.draw(items[i]);

You don't need to use an intermediate array, just the std::vector<Sprite>;

answered on Stack Overflow Aug 20, 2020 by Nelson

User contributions licensed under CC BY-SA 3.0