Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

c++ - How to fix memory leaks in simplest FLTK programm?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...