calling calloc - memory leak valgrind

The following code is an example from the NCURSES menu library. I'm not sure what could be wrong with the code, but valgrind reports some problems. Any ideas...

==4803== 1,049 (72 direct, 977 indirect) bytes in 1 blocks are definitely lost in loss record 25 of 36 ==4803== at 0x4C24477: calloc (vg_replace_malloc.c:418) ==4803== by 0x400E93: main (in /home/gerardoj/a.out) ==4803== ==4803== LEAK SUMMARY: ==4803== definitely lost: 72 bytes in 1 blocks ==4803== indirectly lost: 977 bytes in 10 blocks ==4803== possibly lost: 0 bytes in 0 blocks ==4803== still reachable: 64,942 bytes in 262 blocks

Source code:

#include <curses.h> #include <menu.h> #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define CTRLD 4 char *choices[] = { "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", "Choice 6", "Choice 7", "Exit", } ; int main() { ITEM **my_items; int c; MENU *my_menu; int n_choices, i; ITEM *cur_item; /* Initialize curses */ initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); /* Initialize items */ n_choices = ARRAY_SIZE(choices); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); for (i = 0; i < n_choices; ++i) { my_items[i] = new_item(choices[i], choices[i]); } my_items[n_choices] = (ITEM *)NULL; my_menu = new_menu((ITEM **)my_items); /* Make the menu multi valued */ menu_opts_off(my_menu, O_ONEVALUE); mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item."); mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)"); post_menu(my_menu); refresh(); while ((c = getch()) != KEY_F(1)) { switch (c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case ' ': menu_driver(my_menu, REQ_TOGGLE_ITEM); break; case 10: { char temp[200]; ITEM **items; items = menu_items(my_menu); temp[0] = ''; for (i = 0; i < item_count(my_menu); ++i) if(item_value(items[i]) == TRUE) { strcat(temp, item_name(items[i])); strcat(temp, " "); } move(20, 0); clrtoeol(); mvprintw(20, 0, temp); refresh(); } break; } } unpost_menu(menu); free_item(my_items[0]); free_item(my_items[1]); free_item(my_items[2]); free_item(my_items[3]); free_item(my_items[4]); free_item(my_items[5]); free_item(my_items[6]); free_item(my_items[7]); free_menu(my_menu); endwin(); }

-------------Problems Reply------------

According to the NCURSES Programming Howto, using the menus library requires the following steps:

  • Initialize curses
  • Create items using new_item(). You can specify a name and description for the items.
  • Create the menu with new_menu() by specifying the items to be attached with.
  • Post the menu with menu_post() and refresh the screen.
  • Process the user requests with a loop and do necessary updates to menu with menu_driver.
  • Unpost the menu with menu_unpost()
  • Free the memory allocated to menu by free_menu()
  • Free the memory allocated to the items with free_item()
  • End curses

From what I can tell from your code:

  • You don't unpost the menu (which might cause a leak, or it might just risk garbling the screen).
  • The menu is freed after the items are freed (which I guess may or may not be a problem depending on how ncurses is implemented).
  • Only items 0 and 1 of the 8-element array of items are freed. This is probably a leak.
  • The my_items array of pointers is never freed. This is certainly a leak.

As @lh3 said, compiling with the -g option will let Valgrind give the line number of lost memory.

Edit (in response to your comment): my_items is a dynamically allocated array of pointers to dynamically created menu items. In other words, you have one block of dynamic memory, and it contains a bunch of pointers to a bunch of dynamically allocated ncurses structures (menu items). So, to clean up once you're done, you need to free each of the dynamically allocated ncurses structures, and then you need to free the block of memory that held the pointers to those structures.

In other words, every calloc or malloc needs a free, every new_item needs a free_item, and so on.

for (i = 0; i < n_choices; ++i) {

Something to note with Valgrind (this comes up on the Valgrind user's mailing list often):

still reachable: 64,942 bytes in 262 blocks

This is just referencing blocks that were still reachable in main() at exit, which (under any modern kernel) would just be reclaimed by the OS anyway.

While its good practice to explicitly free() every single allocated block before exit is called, this is not technically leaked memory, since it still could be reached at the time of exit.

Focus on directly, indirectly and possibly lost blocks as Josh Kelly has suggested. This is just a supplement to the answers that have already pointed out the likely sources of leaks.

try running valgrind with --leak-check=full maybe?


Category:c# Views:0 Time:2010-05-27

Related post

  • Memory leaks (Valgrind report)(C++) 2014-06-03

    this is my simple code: #include <cstdlib> #include <cstdlib> #include <cmath> #include <vector> #include <iostream> #include <fstream> #include <cstring> #include <sstream> #include <map> using n

  • c++ destructor memory leak 2011-10-18

    Relatively simple question about handling destructors properly... First I've got a class that's something like this: class Foo { public: ReleaseObjects() { for (std::map<size_t, Object*>::iterator iter = objects.begin(); iter != objects.end();

  • Memory leak reported by valgrind in dlopen? 2009-10-09

    I've been debugging some app lately with valgrind, and I'm getting very weird reports from dlopen. ==1987== 32 bytes in 1 blocks are still reachable in loss record 1 of 2 ==1987== at 0x4C24477: calloc (vg_replace_malloc.c:418) ==1987== by 0x570F31F:

  • Valgrind reporting memory leaks when using API 2010-11-07

    I'm building an application with the Dyninst API, and with a simple Hello World application have many many memory leaks. Dyninst is managing its own memory, in part, and I'm curious if Valgrind would report that memory as "leaked". I'm using C++ and

  • valgrind memory leak errors when using pthread_create 2011-04-10

    I'm writing a program using the pthread library. When i run my program with the command valgrind --leak-check=full, i get the following errors description: ==11784== ==11784== HEAP SUMMARY: ==11784== in use at exit: 4,952 bytes in 18 blocks ==11784==

  • Ubuntu System Monitor and valgrind to discover memory leaks in C++ applications 2011-07-03

    I'm writing an application in C++ which uses some external open source libraries. I tried to look at the Ubuntu System Monitor to have information about how my process uses resources, and I noticed that resident memory continues to increase to very l

  • Valgrind memory leak report differs for executable vs. '.so' library 2012-03-05

    Currently I manage a module of my lab. I checked for memory leaks using valgrind. I found several memory leaks and fixed them. And when I made a '.so' file to provide the module as a library and test code for running the '.so' library, valgrind repor

  • Valgrind has reported "memory leak" when I compiled my program with "-pg" enabled? 2012-12-24

    I've get a memory leak reported by Valgrind with -pg enabled when compiling the following simple code. #include <iostream> #include <boost/filesystem.hpp> #define BOOST_FILESYSTEM_VERSION 3 using boost::filesystem::path; using namespace s

  • memory leaks shown by valgrind to gtk init call 2013-06-01

    I am trying check memory leaks in my code, and valgrind is showing many error. As I have never used valgrind before, I need help. To start with, I am concentrating to the default gtk call's. as coded, memory is leaked from mkbib.c's line number 140.

  • Valgrind Memory Leak Wrong File Trace 2014-08-24

    I'm currently in the memory-leak detection stage of debugging, and am running valgrind --leak-check=full --show-leak-kinds=all on my executable. However, I'm getting some confusing output in a loss record: ==26628== 2 bytes in 1 blocks are indirectly

  • Why is Valgrind stating that my implementation of std::map produces a memory leak? 2009-04-19

    Valgrind is outputting the following: ==14446== 2,976 (176 direct, 2,800 indirect) bytes in 2 blocks are definitely lost in loss record 23 of 33 ==14446== at 0x4C2506C: operator new(unsigned long) (in /usr/lib64/valgrind/amd64-linux/vgpreload_memchec

  • valgrind report memory leak when assign a value to a string 2009-12-14

    Valgrind report memory leak when assign a value to a string I used this simple code to test an memory leak reported by valgrind. /****************************************** * FILE: t3.c * Compiled using : g++ -g t3.c -o t3 * * $ g++ -v * Reading spec

  • Is valgrind crazy or is this is a genuine std map iterator memory leak? 2010-05-20

    Well, I'm very new to Valgrind and memory leak profilers in general. And I must say it is a bit scary when you start using them cause you can't stop wondering how many leaks you might have left unsolved before! To the point, as I'm not an experienced

  • What's up with GTK+ windows having so many memory leaks according to Valgrind? 2010-05-21

    Whenever I load any GTK+-powered application in valgrind, it reports a lot of memory leaks. What's up with that? Is GTK+ buggy? --------------Solutions------------- GTK+ and GLib don't free "allocate once" memory. They follow the paradigm that says i

  • Valgrind C++ memory leaks 2010-11-21

    I have this demonstrable piece of code: #include <cstdlib> #include <cstdio> int main() { int ** matrix = NULL; int c = 1, input = 0; printf("Enter first row of the matrix:\n"); while (!feof(stdin)) { input = fgetc(stdin); matrix = (int**

  • C++ program dies with std::bad_alloc, BUT valgrind reports no memory leaks 2010-12-08

    My program fails with 'std::bad_alloc' error message. The program is scalable, so I've tested on a smaller version with valgrind and there are no memory leaks. This is an application of statistical mechanics, so I am basically making hundreds of obje

  • Prevent valgrind for checking memory leaking in shared libraries linked with our application 2011-01-14

    valgrind-3.6.0.SVN-Debian gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 2.6.35-22-generic I am using valgrind to detect memory errors in our code. However, are application uses some third party libraries (shared library). When we start to capture using v

  • Why doesn't valgrind detect a memory leak in my "test" program? 2011-01-24

    The entire test code is contained in main.cpp as follows: #include <iostream> using std::cout; using std::endl; void f(int i) { int* pi = new int; *pi = i; std::cout << "*pi = " << *pi << std::endl; } int main(int argc, char *

  • Unable to nail memory leak detected by valgrind 2011-01-28

    Below is the pattern of new/delete operators in my program. Valgrind says that memory is "definitely lost". I couldn't quite get where the leak is. Is there something wrong with my usage of new/delete operators. class Generic { GenericInterface *gInt

Copyright (C), All Rights Reserved.

processed in 0.137 (s). 11 q(s)