-
Hello,
I have following code:
#include <SFML/Graphics.hpp>
#include <cstdio>
#include <SFML/System.hpp>
#include <queue>
int getFPS() {
static sf::Clock clock;
static int frames = 0;
static int updateTime = 1000000;
static double fps = 0;
frames++;
if (clock.getElapsedTime().asMicroseconds() > updateTime) {
fps = frames;
frames = 0;
clock.restart();
}
return fps;
}
int main() {
sf::RenderWindow* renderTarget = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Trololo", sf::Style::Close);
char str[50] = "0";
while (true) {
renderTarget->clear();
sprintf(str, "%d\n", getFPS());
sf::String string(str);
sf::Text fpsText(string);
fpsText.setColor(sf::Color(0, 0, 255));
renderTarget->draw(fpsText);
renderTarget->display();
}
return 0;
}
And it starts with 2100 fps. But after 20-30 minutes it's only 1100 fps.
I have no idea what is the reason for it.
-
SFML wasn't build for extremly high FPS. Also in that region each frame needs to be rendered 470us, which is insanely fast any small changes will influence the FPS dramatically. I think it's not that important how high your FPS can get, because you normally don't want your applications to run with such a high FPS - maxing out your CPU for no reason - and if you write an application which needs that highperformance then you'll limit your customer rapidly - not all have such a good CPU/GPU.
-
try to pop events from stack
while (true) {
sf::Event event;
while (renderTarget.pollEvent(event)) {
}
renderTarget->clear();
sprintf(str, "%d\n", getFPS());
sf::String string(str);
sf::Text fpsText(string);
fpsText.setColor(sf::Color(0, 0, 255));
renderTarget->draw(fpsText);
renderTarget->display();
}
-
static int frames = 0;
static int updateTime = 1000000;
static double fps = 0;
Why are you using int and double?
...(sf::VideoMode(800, 600, 32), "Trololo"
R.I.P. Trololo :'(
sf::RenderWindow* renderTarget = new sf::RenderWindow
If you create something with new, you must delete it
And it starts with 2100 fps. But after 20-30 minutes it's only 1100 fps.
Maybe some other program using OpenGL?
-
static int frames = 0;
static int updateTime = 1000000;
static double fps = 0;
Why are you using int and double?
Oh, I used other algorithm earlier and I forgot change this.
sf::RenderWindow* renderTarget = new sf::RenderWindow
If you create something with new, you must delete it
It's not required if object that I create with new is used to the end. It isn't memory leak. All allocated memory is freed automatically when application is closed.
If I lose last pointer to the object then it's memory leak.
And it starts with 2100 fps. But after 20-30 minutes it's only 1100 fps.
Maybe some other program using OpenGL?
Only NetBeans is running.
-
sf::String and sf::Text contain STL containers which make use of the standard new allocator. If you declare those variables in the scope of the while loop you are constantly allocating and freeing memory every frame. Depending on how the OS allocator works this can be stable even after a long time or it can miserably fail due to memory fragmentation which is likely in your case. This is the main reason why all those big name games often make use of memory pools (http://en.wikipedia.org/wiki/Memory_pool). The standard allocators were not made for such aggressive allocation schemes like is needed in many applications nowadays.
If you were to move the declaration of those 2 variables outside of the while loop you would limit the execution of the loop body to only making use of the stack (ignoring the clock.getElapsedTime().asMicroseconds() because there is no way to avoid that). That should theoretically increase the short-term and long-term performance.
It's not required if object that I create with new is used to the end. It isn't memory leak. All allocated memory is freed automatically when application is closed.
If I lose last pointer to the object then it's memory leak.
Turn off the virtual memory manager of your operating system and eventually it will leak to death. It is true that this practice is rooted back in the days of real-mode operating systems, however it is still considered good programming practice to free everything anyways. It is technically considered a leak even if the operating system frees it at the end of execution and any leak tracer like valgrind will report it as such.
-
It's not required if object that I create with new is used to the end. It isn't memory leak. All allocated memory is freed automatically when application is closed.
This is wrong.
The C++ standard isn't required to deallocate the memory upon program shutdown. Although modern operating systems usually do this for memory, this doesn't apply automatically to other resources. Don't forget that delete invokes destructors of class types, which often contain further clean-up code. Maybe your application allocates other resources (e.g. network connections or anything) that need to be closed correctly.
Anyway, using new and delete should generally be avoided in end-user code. There are almost no situations where manual memory management is appropriate and worth the trouble it brings. Here, it is completely unnecessary, as you can just allocate the sf::RenderWindow on the stack:
sf::RenderWindow renderTarget(sf::VideoMode(800, 600, 32), "Trololo", sf::Style::Close);