Hello!

If this mail has reached the list before you can ignore this one. I'm
just re-sending this one because I am unsure whether the last one
reached the list or not.

I am memory checking a program using the popt library but it seems as
there is some kind of memory leak in poptGetNextOpt. I have written a
small test program to show the issue. The program will basically just
take an integer argument and print the integer to the prompt. Of
course, the program works as expected, but valgrind records a leak.

I am attaching the test program. The valgrind output I will post
further down this mail.

I'm using popt version 1.16.

The program was compiled with the following command:
$ gcc -lpopt popt_test.c -o popt_test

$ valgrind --leak-check=full ./popt_test -i 890
==1170== Memcheck, a memory error detector
==1170== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==1170== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==1170== Command: ./popt_test -i 890
==1170==
integer = 890
==1170==
==1170== HEAP SUMMARY:
==1170==     in use at exit: 4 bytes in 1 blocks
==1170==   total heap usage: 6 allocs, 5 frees, 882 bytes allocated
==1170==
==1170== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==1170==    at 0x4A099EB: malloc (in
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==1170==    by 0x4C3372D: expandNextArg (popt.c:696)
==1170==    by 0x4C34FD4: poptGetNextOpt (popt.c:1498)
==1170==    by 0x400873: main (in /home/kalle/popt_test)
==1170==
==1170== LEAK SUMMARY:
==1170==    definitely lost: 4 bytes in 1 blocks
==1170==    indirectly lost: 0 bytes in 0 blocks
==1170==      possibly lost: 0 bytes in 0 blocks
==1170==    still reachable: 0 bytes in 0 blocks
==1170==         suppressed: 0 bytes in 0 blocks
==1170==
==1170== For counts of detected and suppressed errors, rerun with: -v
==1170== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

Should I do something to free the memory or is it some kind of bug?

Thanks in advance,
Karl Lindén
#include <popt.h>
#include <stdio.h>

int
main (int argc, char ** argv)
{
	int integer = 1;
	char c;
	poptContext popt_context;

	struct poptOption popt_options[] = {
		{"integer", 'i', POPT_ARG_INT, &integer, 0,
			"Give me an integer", "INTEGER"},
		POPT_TABLEEND
	};

	popt_context = poptGetContext(NULL, argc, (const char **) argv,
		popt_options, 0);
	while ((c = poptGetNextOpt(popt_context)) >= 0) {
		/* Write some nice code here. A 'switch' clause maybe? */
	}
	poptFreeContext(popt_context);

	printf("integer = %d\n", integer);

	return 0;
}

Reply via email to