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

Author Topic: Memory Leaks  (Read 1484 times)

0 Members and 1 Guest are viewing this topic.

NuclearC__

  • Newbie
  • *
  • Posts: 2
    • View Profile
Memory Leaks
« on: November 14, 2016, 04:44:55 pm »
Hi,

I made an application that draws a lot of outlined circles (100-1000). And it has giant memory leaks (like 4GB after using 30 seconds). It is running 2 loops (event loop and game loop). In game loop it just updates circles position and radius. Sometimes I need to remove some cells. I'm just doing 'cells.erase(cells.begin() + index)' where 'circles' is vector of sf::CircleShape. How can i do it to not leak memory?

Game loop:
struct CellData {
    sf::CircleShape cell;
    sf::Text name;
    sf::Text mass;
    double target_x;
    double target_y;
    double target_size;
};

std::vector<std::pair<..::Client::ClientCell, CellData>> cells;

void draw_cells()
{
    std::sort(cells.begin(), cells.end(), [](auto &left, auto &right) {
        return left.second.target_size < right.second.target_size;
    });

    for (int i = 0; i < cells.size(); i++)
    {
        cells[i].second.cell.setRadius(cells[i].second.target_size);
        cells[i].second.cell.setPosition(sf::Vector2f(cells[i].second.target_x - cells[i].second.target_size, cells[i].second.target_y - cells[i].second.target_size));

        if (!(cells[i].first.flags & 0x04))
        {
            cells[i].second.cell.setFillColor(sf::Color(cells[i].first.color[0], cells[i].first.color[1], cells[i].first.color[2]));
        }

        if (cells[i].first.size > 20)
        {
            cells[i].second.cell.setOutlineColor(sf::Color(cells[i].first.color[0] / 2, cells[i].first.color[1] / 2, cells[i].first.color[2] / 2));
            cells[i].second.cell.setOutlineThickness(10);
        }
       
        wnd->draw(cells[i].second.cell);

        ...
    }
}
 

Cell remove code:
        for (int a = 0; a < remove_cells.size(); a++)
        {
            for (int i = 0; i < cells.size(); i++)
            {
                if (cells[i].first.id == remove_cells[a]) {
                    cells.erase(cells.begin() + i);
                }
            }
        }
 

Thanks.
« Last Edit: November 14, 2016, 05:08:12 pm by NuclearC__ »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Memory Leaks
« Reply #1 on: November 14, 2016, 05:01:07 pm »
Without code, how should we tell? ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Leaks
« Reply #2 on: November 14, 2016, 07:05:17 pm »
Is remove_cells cleared sometimes?

By the way, the correct way to erase elements while iterating is:

for (auto it = cells.begin(); it != cells.end(); )
{
    if (it->first.id == remove_cells[a])
        it = cells.erase(it);
    else
        ++it;
}

And remove_cells could be a sorted container, so that you could directly find the id and remove one of these two loops.
Laurent Gomila - SFML developer

NuclearC__

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Memory Leaks
« Reply #3 on: November 14, 2016, 07:16:42 pm »
Is remove_cells cleared sometimes?

By the way, the correct way to erase elements while iterating is:

for (auto it = cells.begin(); it != cells.end(); )
{
    if (it->first.id == remove_cells[a])
        it = cells.erase(it);
    else
        ++it;
}

And remove_cells could be a sorted container, so that you could directly find the id and remove one of these two loops.

Thanks for your reply.

Yes, remove_cells is cleared after removing all cells in that array. Well I tried your code and got same result. 2GB Memory after running it 14 seconds. It was only drawing about 20 cells with radius 300-1000.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Leaks
« Reply #4 on: November 14, 2016, 09:07:57 pm »
Quote
Well I tried your code and got same result
Then you're not showing enough of your code. You should try to reduce it to a complete and minimal code that still produces the leak. Instead of posting parts of your big projects, which may take quite some time until someone finds the leak.
Laurent Gomila - SFML developer