微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我应该调用从initscr返回的delwinWINDOW *吗?

如何解决我应该调用从initscr返回的delwinWINDOW *吗?

所以我在学习如何使用ncurses时遇到了this link(可能不相关)。我很好奇ncurses如何管理其内存,我在valgrind下运行了程序,以查看从此代码调用initscr()直到调用endwin()的ncurses期间使用了多少内存。下面在C

#include <curses.h>
#include <stdlib.h>

// A program to move to some position(y,x) with
// 0 to LInes - 1 and 0 to COLS - 1
// and tell the user to input something.
// It ends inputting something until user presses
// ESC button.

int c;              // the char
int main()  
{
    initscr(); // this is the first thing to run if you're using ncurses.

    nonl();          // tells the ncurses not to put newline after printing something.
    cbreak();        // no waiting for newline to be inputted. (raw mode)
    echo();          // echo characters.
    
    for( ; ; ) {
        int y = rand() % LInes; // get random value from 0 to LInes - 1.
        int x = rand() % COLS;  // get random value from 0 to COLS - 1.
        move(y,x);             // move cursor to (y,x).
        c = getch();            // to get character
        if (c == 27) break;     // if the user pressed ESC (27)
        // refresh();              // refresh the window to get update.
                                // i realized that it doesn't need to refresh
                                // since the input is already put into the WINDOW
    }
    
    endwin();                   // end session of the ncurses window
                                // also restores the state of the terminal.
}

使用该命令gcc -g file.c -lncurses编译的

,并在valgrind ./a.out下运行(输入3'a),告诉valgrind发现了一些可到达的内存泄漏,如下所示:

==30413== Memcheck,a memory error detector
==30413== copyright (C) 2002-2017,and GNU GPL'd,by Julian Seward et al.
==30413== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==30413== Command: ./a.out
==30413== 
==30413== 
==30413== HEAP SUMMARY:
==30413==     in use at exit: 42,466 bytes in 105 blocks
==30413==   total heap usage: 115 allocs,10 frees,51,457 bytes allocated
==30413== 
==30413== LEAK SUMMARY:
==30413==    definitely lost: 0 bytes in 0 blocks
==30413==    indirectly lost: 0 bytes in 0 blocks
==30413==      possibly lost: 0 bytes in 0 blocks
==30413==    still reachable: 42,466 bytes in 105 blocks
==30413==         suppressed: 0 bytes in 0 blocks
==30413== Rerun with --leak-check=full to see details of leaked memory
==30413== 
==30413== For lists of detected and suppressed errors,rerun with: -s
==30413== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我工作的相似代码(通过添加delwin()返回的initscr())并在valgrind下运行,显示了较少的可访问内存泄漏。下面的代码

#include <curses.h>
#include <stdlib.h>

// A program to move to some position(y,x) with
// 0 to LInes - 1 and 0 to COLS - 1
// and tell the user to input something.
// It ends inputting something until user presses
// ESC button.

int c;              // the char
WINDOW* win;        // a pointer to a WINDOW (not neccessary,i guess)
int main()  
{
    win = initscr(); // this is the first thing to run if you're using ncurses.

    nonl();          // tells the ncurses not to put newline after printing something.
    cbreak();        // no waiting for newline to be inputted. (raw mode)
    echo();          // echo characters.
    
    for( ; ; ) {
        int y = rand() % LInes; // get random value from 0 to LInes - 1.
        int x = rand() % COLS;  // get random value from 0 to COLS - 1.
        move(y,x).
        c = getch();            // to get character
        if (c == 27) break;     // if the user pressed ESC (27)
        // refresh();              // refresh the window to get update.
                                // i realized that it doesn't need to refresh
                                // since the input is already put into the WINDOW
    }
    delwin(win);                // deletes and frees WINDOW ????????
    endwin();                   // end session of the ncurses window
                                // also restores the state of the terminal.
}
valgrind下运行的

显示以下输出

==30798== Memcheck,a memory error detector
==30798== copyright (C) 2002-2017,by Julian Seward et al.
==30798== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==30798== Command: ./a.out
==30798== 
==30798== 
==30798== HEAP SUMMARY:
==30798==     in use at exit: 34,298 bytes in 79 blocks
==30798==   total heap usage: 115 allocs,36 frees,457 bytes allocated
==30798== 
==30798== LEAK SUMMARY:
==30798==    definitely lost: 0 bytes in 0 blocks
==30798==    indirectly lost: 0 bytes in 0 blocks
==30798==      possibly lost: 0 bytes in 0 blocks
==30798==    still reachable: 34,298 bytes in 79 blocks
==30798==         suppressed: 0 bytes in 0 blocks
==30798== Rerun with --leak-check=full to see details of leaked memory
==30798== 
==30798== For lists of detected and suppressed errors,rerun with: -s
==30798== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我的结论是使用delwin()返回的initscr()可以减少泄漏。我们还应该真正处理WINDOW中初始化的initscr()吗?任何答案表示赞赏。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。