1

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?

Bogdasar
  • 181
  • 1
  • 1
  • 9
  • `On the other hand, this can be said to be a classic example of a FLTK program.` not all examples are good examples. The example relies on the OS releasing all memory allocated by the application when the application exits. But that does not mean that this is good practice. – t.niese Jan 24 '21 at 21:20
  • I assume you can put the value returned from `Fl::run();` in a variable then `delete window;` and return that value to end main. – drescherjm Jan 24 '21 at 22:28
  • @drescherjm I tried, not work – Bogdasar Jan 25 '21 at 07:31

2 Answers2

2

As mentioned by @drescherjrn, you can delete window after run. Something like

int main()
{
    Fl_Window *window = new Fl_Window(win_w, win_h, "Hello, World!");
    window->end();
    window->show();
    int rv = Fl::run();
    delete window;
    return rv;
}

Alternatively, C++ has a mechanism for this: auto_ptr (http://www.cplusplus.com/reference/memory/auto_ptr/auto_ptr/)

#include <memory>
int main()
{
    std::auto_ptr<Fl_Window> window(new Fl_Window(win_w, win_h, "Hello, World!"));
    window->end();
    window->show();
    return Fl::run();
}
cup
  • 7,589
  • 4
  • 19
  • 42
  • `auto_ptr` was deprecated in C++11 and removed in C++17. A much better response here would be for `std::unique_ptr` – lefticus Mar 17 '22 at 01:11
1

You can try:

int main()
{
    Fl_Window *window = new Fl_Window(win_w, win_h, "Hello, World!");
    window->end();
    window->show();
    int retval = 1;
    if (Fl::run())
        retval =0;
    delete window ;
    return retval ;
}