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

Author Topic: Not sure if bug or not, getting division by zero...  (Read 2038 times)

0 Members and 1 Guest are viewing this topic.

jokoon

  • Newbie
  • *
  • Posts: 35
    • View Profile
Not sure if bug or not, getting division by zero...
« on: November 14, 2013, 03:10:49 pm »
Using SFML 2.1 with MSVC 2012 on windows 7 64 bits. Can't really remember if I'm using the 64 bits version of the SFML lib.

struct menu
{
    //map<string,Sprite> icons;
    vector<Sprite> icons;
    vector<Texture> tx;
    vector<Text> texts;
    Vec2 origin;
    vector<string> filenames;
    void init()
    {
        for(int i = 0; i < 50; ++i)
        {
            tx.push_back(Texture());
            if(!tx.back().loadFromFile("png/beans.png")) cout << "could not open breeagfdso" << endl;
            icons.push_back(Sprite(tx.back()));
        }
        cout << icons.size() << " elems" << endl;
        float
            c_x = icons.begin()->getTextureRect().width+2,
            c_y = icons.begin()->getTextureRect().height+2;
        cout << "cx cy = " << c_x << " " << c_y << endl;
        int i = 0;
        for(auto&a:icons)
        {
            a.setPosition(Vec2((i%4)*c_x, (i/4)*c_y)+origin);
            ++i;
        }

    }
    void draw(RenderWindow&window)
    {
        for(auto&a:icons)
            window.draw(a);
    }
};

I think the code is pretty straightforward. I instantiate a menu and initialize it like this:

struct game
{
menu m;
void init()
{
    m.origin=Vector2f(10,10);
    m.init();
}

This is the error: Integer division by zero.

This previous code crashes if I set i < 3, the third displays fine, and 2 others are white squares. If I use only one texture and many sprites by just putting those 2 lines out of the loop like this:
        tx.push_back(Texture());
        if(!tx.back().loadFromFile("png/beans.png")) cout << "could not open breeagfdso" << endl;
        for(int i = 0; i < 50; ++i)
        {
            icons.push_back(Sprite(tx.back()));
        }
It runs fine and I see those icons displayed many times in rows.

I had a discussion lately on IRC about this, I was told I should use unique_ptr, but I don't think that's a solution since I use plain vectors, not vectors of pointers.

Can somebody tell me if it's a bug, or if the Texture class should not be used plain in a vector ?

Here is the call stack : http://pastebin.com/47bJmH1c

I remember outputing the size of the sprite rectangle like it was advised in one thread, I got some very large int number, but I don't understand why.
« Last Edit: November 14, 2013, 03:15:17 pm by jokoon »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Not sure if bug or not, getting division by zero...
« Reply #1 on: November 15, 2013, 09:03:01 am »
As discussed on IRC the texture's internal size seems to be set to 0, while the texture is somehow still "active", thus when the draw functions tries to divide by the size of 0, you'll get a division by 0 error.

Even though we now know how it happens, I still don't know why exactly. You essentially should use a unique_ptr (it's not bad, it has nothing to do with Java and it's also not unnecessary), so when the vector copies things around it only copies the unique_ptr and not the full texture.

But here's the thing that I don't really get and we at first thought was an bug in SFML, while the texture implements a copy constructor, it somehow fails to copy everything properly, at least that's the only explanation why the texture's size is suddenly 0.
Any ideas on that Laurent?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Not sure if bug or not, getting division by zero...
« Reply #2 on: November 15, 2013, 09:58:16 am »
This looks a litte bit strange to me:

if(!tx.back().loadFromFile("png/beans.png")) cout << "could not open breeagfdso" << endl;
            icons.push_back(Sprite(tx.back()));

So even if the texture cannot be loaded, you create a sprite for it.
Should IMHO rather be something like this:

if(!tx.back().loadFromFile("png/beans.png")) cout << "could not open breeagfdso" << endl;
            else icons.push_back(Sprite(tx.back()));



Did you try to reserve Memory for your vector?
The way it is know, it might happen that your textures get reallocated 6 Times (when i = 2, 3, 5, 9, 17 and 33)

void init()
    {
        tx.reserve(50);
        for(int i = 0; i < 50; ++i)

« Last Edit: November 15, 2013, 10:00:03 am by Ghosa »