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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - NuclearC__

Pages: [1]
1
Graphics / Re: Memory Leaks
« 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.

2
Graphics / 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.

Pages: [1]