Mailinglist Archive: opensuse-programming (23 mails)

< Previous Next >
Re: [suse-programming-e] False memleak detected?
  • From: peter burden <peter.burden@xxxxxxxxx>
  • Date: Sun, 09 Jul 2006 16:17:59 +0100
  • Message-id: <44B11E27.1050701@xxxxxxxxx>
Jerry Feldman wrote:
On Monday 19 June 2006 10:56 am, Verdi March wrote:
Hi,

On Monday 19 June 2006 22:09, Jerry Feldman wrote:
No. That is because the pointer is available in main. If you allocate
memory inside of a function, and return from that function without
freeing that memory or without providing a pointer to that memory,
that is unreachable.
I tried the above with a simpler testcase:

int main() {
int *iptr
iptr (int *) malloc(sizeof(int));
return 0;
}

and valgrind still detect a memory leak. Obviously I hold a
pointer (iptr) to the allocated memory, or do I misunderstand
your explanation?
This is a memory leak because you return from main() without freeing memory.
The following code is NOT a memory leak:
int main() {
int *iptr
iptr (int *) malloc(sizeof(int));
free(iptr);
return 0;

However the following reports the block as "still reachable"

#include <stdlib.h>
static int *iptr;
int main()
{
iptr = (int *)malloc(sizeof (int));
return 0;
}

As commented by Phil Betts, valgrind checks the state of
memory AFTER main() exits.




< Previous Next >
Follow Ups