memleaks(3NCURSES)

πŸ’– memleaks β€” the "where did my memory GO" girlies πŸ’–

okay bestie, this is the ncurses page about sniffing out memory leaks β€” it's a library-calls page from the ncurses manual, and we're gonna go through it together, no skips, no cap. ✨


🌸 NAME 🌸

exit_curses, exit_terminfo β€” check for memory leaks in curses πŸ•΅οΈβ™€οΈπŸ’…


✨ SYNOPSIS ✨

#include <curses.h>
void exit_curses(int code);

#include <term.h>
void exit_terminfo(int code);

/* deprecated (intentionally not declared in curses.h or term.h) */
void _nc_freeall(void);
void _nc_free_and_exit(int code);
void _nc_free_tinfo(int code);

literally just copy that, don't freestyle it bestie 🫢


πŸ’œ DESCRIPTION πŸ’œ

So these functions exist to make memory-leak analysis in the ncurses library way less of a nightmare. That's the whole vibe. 🧚

Here's the thing β€” any implementation of curses is not allowed to free the memory tied to a screen, because even after you call endwin(3NCURSES), that memory has to still be there for the next call to refresh(3NCURSES). There are also chunks of memory being held purely for performance reasons. Which makes leak-checking a curses app lowkey impossible normally. 😭

But!! If you're running the specially configured debugging version of the ncurses library, your app can call functions that free those chunks of memory, which simplifies the whole memory-leak-checking process. slay ✨

Some of these functions have the _nc_ prefix because they are not intended for use in the non-debugging library. Iconic of them, honestly.

  • _nc_freeall β€” frees (almost) all of the memory allocated by ncurses. Note the "(almost)", she's honest about it. πŸ’…
  • _nc_free_and_exit β€” frees the memory allocated by ncurses (just like _nc_freeall), and then exits the program. It's preferred over _nc_freeall because some of that memory may actually be required to keep the application running. Simply exiting (with the given exit-code) is safer. πŸšͺ✨
  • _nc_free_tinfo β€” use this one if only the low-level terminfo functions (and the corresponding library) are being used. Like _nc_free_and_exit, it exits the program after freeing memory. πŸ¦‹

The functions prefixed _nc are normally not available β€” they have to be configured into the library at build time using the --disable-leaks option. That option compiles in code that frees memory which normally would not be freed. πŸ”§

And then: exit_curses and exit_terminfo call _nc_free_and_exit and _nc_free_tinfo if the library is configured to support memory-leak checking. If the library is not configured to support memory-leak checking, they just simply call exit. casual. πŸ’€


πŸŽ€ RETURN VALUE πŸŽ€

These functions do not return a value. nothing, nada, zero, bye πŸ™ˆ


🌈 PORTABILITY 🌈

Bestie, listen β€” these functions are not part of X/Open Curses, and other implementations of curses don't provide a similar feature either. Not a thing anywhere else. Periodt. πŸ’…

But! In any implementation of X/Open Curses, an application can free part of the memory allocated by curses:

  • The portable part of exit_curses can be freed using delscreen, passing the SCREEN pointer returned by newterm.

In some implementations, there's a global variable sp which could be used, e.g., if the screen were only initialized using initscr.

  • The portable part of exit_terminfo can be freed using del_curterm.

In this case, there's a global variable cur_term which can be used as parameter.

iykyk πŸ‘‘


πŸ‘‘ SEE ALSO πŸ‘‘

ncurses(3NCURSES), initscr(3NCURSES), terminfo(3NCURSES) β€” go read those next, bestie, they're the whole friend group 🫢


mwah πŸ’‹ that was the memleaks page, 3NCURSES section, from ncurses 6.6, dated 2024-03-16. thank you for coming to my ted talk, go free your memory (responsibly), and remember: it's giving no leaks, only slay βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from