Member Avatar for hohoho89

Ok so now be patient and respectful because this is the first time I've used ncurses.

But here's my story.

I'm making a application using the ncurses. and I just realized one thing.

Whenever I used initscr(). I get approx over 70 000 memory leakage.
Any suggestion on what I should do?

the valgrind displays the memory leak.
I would be happy if any of you out there would answer this.

There is very little context with which to answer this question.
Where does valgrind say the leak is? Your code, within the ncurses library? Do you just get a certain amount of increase in memory size, and not necessarily leakage, per se? Do you call endwin() ?
Does the leak exists for a minimal program? For instance, if all you do is initscr() and endwin() ?

Member Avatar for hohoho89

Well, I do a little more than that, I could at least copy n paste the result in valgrind
first then the little pseudocode in C.

==24810== HEAP SUMMARY:
==24810==     in use at exit: 78,519 bytes in 287 blocks
==24810==   total heap usage: 328 allocs, 41 frees, 99,177 bytes allocated
==24810== 
==24810== LEAK SUMMARY:
==24810==    definitely lost: 0 bytes in 0 blocks
==24810==    indirectly lost: 0 bytes in 0 blocks
==24810==      possibly lost: 0 bytes in 0 blocks
==24810==    still reachable: 78,519 bytes in 287 blocks
==24810==         suppressed: 0 bytes in 0 blocks
==24810== Rerun with --leak-check=full to see details of leaked memory
==24810== 
==24810== For counts of detected and suppressed errors, rerun with: -v
==24810== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 16 from 7)

Pseudocode(NOTICE! The exit_client contains delwin() and endwin())

/*ncurses window*/
WINDOW *inputWin;
WINDOW *outputWin;



void check_connection() {
    signal(SIGINT, exit_client);
    int ret;
    char machine[BUFFERSIZE];

    /*ncurses mode*/

    initscr();
    keypad(stdscr, TRUE);
    nonl();
    cbreak();
    echo();
    outputWin = newwin(LINES - 1, COLS, 0, 0);
    inputWin = newwin(1, COLS, LINES - 1, 0);


    wprintw(outputWin, "WELCOME TO CLIENT TERMINAL\n");
    wrefresh(outputWin);
    wmove(inputWin, 0, 0);
    memset(machine, 0, sizeof (machine));
    //fgets(buffer, BUFFERSIZE, stdin);

    wgetstr(inputWin, buffer);
    wclear(inputWin);
    wrefresh(inputWin);

    char *command = strtok(buffer, "\n");
    ret = sscanf(command, "MONITOR %s %d %s %% %s sek", machine, &portnumber, thresh, sec);

    if (ret == 4) {

        connect_to_server(machine);

    } else {

        delwin(inputWin);
        delwin(outputWin);

        endwin();

    }
}

PS! I skimmed main() and the other functions. This is the function that has the initscr()
and exit_client() have the endwin() inside if my application types ctrl-c
I'm quite uncertain where the valgrind mention the memory leaks, but everytime I turn initscr() and even though I have the endwin() on the function for example. I still get 78 kb unreachable bytes

There is very little context with which to answer this question.
Where does valgrind say the leak is? Your code, within the ncurses library? Do you just get a certain amount of increase in memory size, and not necessarily leakage, per se? Do you call endwin()?
Does the leak exists for a minimal program? For instance, if all you do is initscr() and endwin()?

==24810== LEAK SUMMARY:
==24810== definitely lost: 0 bytes in 0 blocks
==24810== indirectly lost: 0 bytes in 0 blocks
==24810== possibly lost: 0 bytes in 0 blocks
...
==24810== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 16 from 7)

valgrind isn't reporting any leaks.

"still reachable" means your program is probably ok -- it didn't free some memory it could have. This is quite common and often reasonable. Don't use --show-reachable=yes if you don't want to see these reports.

This is probably all ncurses-related. You can get the full report if you do something like: valgrind --leak-check=full --show-reachable=yes

Member Avatar for hohoho89

Well, There should be a function for cleaning up the reachable ones. Something like --disable-leaks or something like that. But well, I don't know if any other have thought about this before that they get so many reachable bytes

There is another [ncurses] FAQ that explains the reasoning behind this: leak FAQ

Member Avatar for hohoho89

Well, there should be like "gcc -g -00 - o client client.c && valgrind --disable-leaks ./client"
something like that right?

There is another [ncurses] FAQ that explains the reasoning behind this: leak FAQ

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.