I've got a problem with memory in C++ program with FLTK.
In this first case:
#include <Fl/Fl.H>
#include <Fl/Fl_Window.H>
enum {
win_w = 640,
win_h = 480,
btn_w = 80,
btn_h = 30
};
int main()
{
Fl_Window *window = new Fl_Window(win_w, win_h, "Hello, World!");
window->end();
window->show();
return Fl::run();
}
valgrind
said that:
HEAP SUMMARY:
==2746== in use at exit: 711,091 bytes in 846 blocks
==2746== total heap usage: 13,330 allocs, 12,484 frees, 2,751,634 bytes allocated
==2746==
==2746== LEAK SUMMARY:
==2746== definitely lost: 0 bytes in 0 blocks
==2746== indirectly lost: 0 bytes in 0 blocks
==2746== possibly lost: 0 bytes in 0 blocks
==2746== still reachable: 711,091 bytes in 846 blocks
==2746== suppressed: 0 bytes in 0 blocks
==2746== Rerun with --leak-check=full to see details of leaked memory
If I will add a Fl_Button
to this programm, we gonna have much more memory leaking.
...
Fl_Window *window = new Fl_Window(win_w, win_h, "Hello, World!");
Fl_Button *btn = new Fl_Button(win_w / 2 - btn_w / 2, win_h / 2 - btn_h / 2, btn_w, btn_h, "Button!");
window->end();
...
==2791== HEAP SUMMARY:
==2791== in use at exit: 1,065,964 bytes in 6,005 blocks
==2791== total heap usage: 25,233 allocs, 19,228 frees, 6,637,915 bytes allocated
==2791==
==2791== LEAK SUMMARY:
==2791== definitely lost: 5,376 bytes in 19 blocks
==2791== indirectly lost: 3,371 bytes in 128 blocks
==2791== possibly lost: 0 bytes in 0 blocks
==2791== still reachable: 1,057,217 bytes in 5,858 blocks
==2791== suppressed: 0 bytes in 0 blocks
==2791== Rerun with --leak-check=full to see details of leaked memory
On the one hand, this is normal, because we do not free memory. On the other hand, this can be said to be a classic example of a FLTK program. Are memory leaks normal in FLTK? And if it's OK, why shouldn't this lead to problems if the program has been running continuously for a very long time?
question from:
https://stackoverflow.com/questions/65875954/how-to-fix-memory-leaks-in-simplest-fltk-programm