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

Author Topic: Crash after too many objects in screen  (Read 2645 times)

0 Members and 1 Guest are viewing this topic.

GamerH2

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Crash after too many objects in screen
« on: October 12, 2017, 02:45:58 am »
My game crashes after my objects vector's size gets higher than 127.

This is my code

--- Main loop function
Code: [Select]
// Set the FPS limit of the window
m_window.setFramerateLimit(60);

// Activate the window
m_window.setActive(true);

// Disable lighting
glDisable(GL_LIGHTING);

// Configure the viewport (the same size as the window)
glViewport(0, 0, m_window.getSize().x, m_window.getSize().y);

// Enable OpenGL
glEnable(GL_TEXTURE_2D);

// Load all scenes
SceneTest sc_0;
scenes.push_back(&sc_0);

sf::Texture sortOfATex;
sortOfATex.loadFromFile("data/uglie.gif");

// Main game loop
while (m_window.isOpen())
{
// Event
sf::Event event;

while (m_window.pollEvent(event))
{
// Close the game when the [X] button is clicked
if (event.type == sf::Event::Closed)
m_window.close();
}

// Clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Run the scene
scenes[currentScene]->run(m_window);

if (sf::Keyboard::isKeyPressed(sf::Keyboard::B))
{
GameObject obj(sortOfATex);
objects.push_back(obj);
}

int8_t i;

for (const auto& x : objects)
{
objects[i].update();
objects[i].draw(m_window);
i++;
}

i = 0;

// Display the things on the window
m_window.display();

}
--- GameObject
Source file
Code: [Select]
#include "GameObject.hpp"

GameObject::GameObject(sf::Texture& text)
    : spr(text)
{}

GameObject::~GameObject() {}

void GameObject::update()
{
    spr.move(1, 0);
}

void GameObject::draw(sf::RenderWindow& w)
{
    w.draw(this->spr);
}
Header
Code: [Select]
#ifndef GAMEOBJECT_HPP
#define GAMEOBJECT_HPP
#include <SFML/Graphics.hpp>

class GameObject
{
    public:
        GameObject(sf::Texture& text);
        virtual ~GameObject();
        virtual void    update();
        virtual void    draw(sf::RenderWindow& w);
    private:
        sf::Sprite      spr;
};

#endif // GAMEOBJECT_HPP

Thanks for reading.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Crash after too many objects in screen
« Reply #1 on: October 12, 2017, 07:39:32 am »
And what's the call stack?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

GamerH2

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Crash after too many objects in screen
« Reply #2 on: October 12, 2017, 12:42:19 pm »
Do you mean that's happening stack overflow?
Then, how i can fix it? Because the game crashes after the first object when i do this:
Code: [Select]
std::vector<GameObject*> objects;

if (sf::Keyboard::isKeyPressed(sf::Keyboard::B))
{
    GameObject* obj = new GameObject(sortOfATex);
    objects.push_back(obj);
    delete obj;
}

int8_t i;

for (const auto& x : objects)
{
objects[i]->update();
objects[i]->draw(m_window);
i++;
}

i = 0;


Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Crash after too many objects in screen
« Reply #3 on: October 12, 2017, 12:49:51 pm »
Do you have any references or pointers to elements of your vector?

When you add an element to your vector and it reaches a size larger than n², it resizes and might get moved to an other space in memory, invalidating your previous pointer.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Crash after too many objects in screen
« Reply #4 on: October 12, 2017, 12:51:49 pm »
Quote
Do you mean that's happening stack overflow?
When your code crashes, you usually run it with the debugger, which gives you plenty of information about the error. Among them, one of the most useful is the call stack, which is the sequence of function calls that led to the location of the crash. Knowing what crashed and at which location in source code, is a very good starting point for debugging a crash ;)

Quote
the game crashes after the first object when i do this
You delete the object that you just added to your vector, then you try to use it. Accessing a deleted memory location is undefined behaviour.
Laurent Gomila - SFML developer

GamerH2

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Crash after too many objects in screen
« Reply #5 on: October 12, 2017, 01:01:47 pm »
I ran the debugger, and after the crash, this appear in the debugger
Code: [Select]
Program received signal SIGSEGV, Segmentation fault.
At Core.cpp:73

Core.cpp:73 is the  objects.update();
« Last Edit: October 12, 2017, 01:03:54 pm by GamerH2 »

GamerH2

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Crash after too many objects in screen
« Reply #6 on: October 12, 2017, 01:16:32 pm »
I found what's happening!

Code: [Select]
int8_t i;

for (const auto& x : objects)
{
objects[i].update();
objects[i].draw(m_window);
i++;
}

i = 0;

I declared a signed int8_t variable, which explains why the object limit was being 127!!!
So, i changed it to a unsigned short, and now i can have 65535 objects in screen.

Thanks for the help guys!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Crash after too many objects in screen
« Reply #7 on: October 12, 2017, 02:18:20 pm »
That's a very complicated and unintuitive way to iterate on your object array.

for (const auto& x : objects)
{
    x.update();
    x.draw(m_window);
}

No more limit.
Laurent Gomila - SFML developer

 

anything