Mailinglist Archive: opensuse-programming (23 mails)

< Previous Next >
Re: [suse-programming-e] False memleak detected?
  • From: Jerry Feldman <gaf@xxxxxxx>
  • Date: Sun, 9 Jul 2006 11:53:17 -0400
  • Message-id: <20060709115317.503ef30c@xxxxxxxxxxx>
On Sun, 09 Jul 2006 16:17:59 +0100
peter burden <peter.burden@xxxxxxxxx> wrote:

> 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.
Yes, it is reachable because iptr is a static variable. But, it is
still a memory leak.

--
Jerry Feldman <gaf@xxxxxxx>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
< Previous Next >
References