|
From: Verdi M. <ver...@co...> - 2006-06-19 16:34:53
|
Hi,
valgrind reported the following as "definite lost". I thought this is
the case of "still reachable", because I still hold a pointer to the
allocated memory? At least this is what I understand from the user
manual.
I'm using suse 10.1 (32-bit version): valgrind-3.2.0-11.1,
kernel-smp-2.6.16.13-4, glibc-2.4-31, and gcc-4.1.0-25.
/***********************************/
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ip;
ip =(int *) malloc(sizeof(int));
return 0;
}
/***********************************/
/*********************************************************************/
$ valgrind --leak-check=yes ./a.out
==27277== Memcheck, a memory error detector.
==27277== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et
al.
==27277== Using LibVEX rev 1606, a library for dynamic binary
translation.
==27277== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==27277== Using valgrind-3.2.0, a dynamic binary instrumentation
framework.
==27277== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et
al.
==27277== For more details, rerun with: -v
==27277==
==27277==
==27277== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
==27277== malloc/free: in use at exit: 4 bytes in 1 blocks.
==27277== malloc/free: 1 allocs, 0 frees, 4 bytes allocated.
==27277== For counts of detected errors, rerun with: -v
==27277== searching for pointers to 1 not-freed blocks.
==27277== checked 56,636 bytes.
==27277==
==27277== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==27277== at 0x4021396: malloc (vg_replace_malloc.c:149)
==27277== by 0x80483D0: main (cobi.c:6)
==27277==
==27277== LEAK SUMMARY:
==27277== definitely lost: 4 bytes in 1 blocks.
==27277== possibly lost: 0 bytes in 0 blocks.
==27277== still reachable: 0 bytes in 0 blocks.
==27277== suppressed: 0 bytes in 0 blocks.
==27277== Reachable blocks (those to which a pointer was found) are not
shown.
==27277== To see them, rerun with: --show-reachable=yes
/*********************************************************************/
--
Regards,
Verdi
|
|
From: Tom H. <to...@co...> - 2006-06-19 16:39:10
|
In message <200...@co...>
Verdi March <ver...@co...> wrote:
> valgrind reported the following as "definite lost". I thought this is
> the case of "still reachable", because I still hold a pointer to the
> allocated memory? At least this is what I understand from the user
> manual.
>
> I'm using suse 10.1 (32-bit version): valgrind-3.2.0-11.1,
> kernel-smp-2.6.16.13-4, glibc-2.4-31, and gcc-4.1.0-25.
>
>
> /***********************************/
> #include <stdio.h>
> #include <stdlib.h>
>
> int main() {
> int *ip;
> ip =(int *) malloc(sizeof(int));
> return 0;
> }
> /***********************************/
The only pointer to that memory is "ip" and once you return from
main that variables goes out of scope and you no longer have any
valid pointers to the memory.
Tom
--
Tom Hughes (to...@co...)
http://www.compton.nu/
|
|
From: Verdi M. <ver...@co...> - 2006-06-19 17:15:35
|
Hi,
On Tuesday 20 June 2006 00:38, Tom Hughes wrote:
> > /***********************************/
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main() {
> > int *ip;
> > ip =(int *) malloc(sizeof(int));
> > return 0;
> > }
> > /***********************************/
>
> The only pointer to that memory is "ip" and once you return from
> main that variables goes out of scope and you no longer have any
> valid pointers to the memory.
I see. But when main() returns, the program will terminate and the
memory being used will be released anyway. The reason I was hoping
this being a case of "reachable" is that often I don't explicitly
free() pointers which will be used until a program terminates.
Just for curiosity, I changed the program to "exit(0)" instead
of "return 0", and valgrind now reports "unreachable" instead
of "definite lost". Does it mean that I should always use "exit"
to terminate programs? I thought that in main(), both "return" and
"exit" achieve the same effect.
--
Regards,
Verdi
|
|
From: Bryan M. <om...@br...> - 2006-06-19 18:46:30
|
Verdi March wrote:
> Hi,
>
> On Tuesday 20 June 2006 00:38, Tom Hughes wrote:
>>> /***********************************/
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> int main() {
>>> int *ip;
>>> ip =(int *) malloc(sizeof(int));
>>> return 0;
>>> }
>>> /***********************************/
>> The only pointer to that memory is "ip" and once you return from
>> main that variables goes out of scope and you no longer have any
>> valid pointers to the memory.
>
> I see. But when main() returns, the program will terminate and the
> memory being used will be released anyway. The reason I was hoping
> this being a case of "reachable" is that often I don't explicitly
> free() pointers which will be used until a program terminates.
>
> Just for curiosity, I changed the program to "exit(0)" instead
> of "return 0", and valgrind now reports "unreachable" instead
> of "definite lost". Does it mean that I should always use "exit"
> to terminate programs? I thought that in main(), both "return" and
> "exit" achieve the same effect.
>
No - exit your program in the way your program needs to exit.
What you probably don't realise is that there is a lot of code executed
before your function main() is called. Likewise, there is a significant
amount of code executed after your function main() exits before the
program actually exits.
Thus, as Tom says, your only pointer to the memory block goes out of
scope before the program exists and this is a leak.
Bryan "Brain Murders" Meredith
|
|
From: Tom H. <to...@co...> - 2006-06-19 19:41:42
|
In message <200...@co...>
Verdi March <ver...@co...> wrote:
> On Tuesday 20 June 2006 00:38, Tom Hughes wrote:
>
> > The only pointer to that memory is "ip" and once you return from
> > main that variables goes out of scope and you no longer have any
> > valid pointers to the memory.
>
> I see. But when main() returns, the program will terminate and the
> memory being used will be released anyway. The reason I was hoping
> this being a case of "reachable" is that often I don't explicitly
> free() pointers which will be used until a program terminates.
It is true that it is more of a technical leak (although maybe not
in a multithreaded program) but a static variable is better if you
really want a lifetime that matches that of the program.
> Just for curiosity, I changed the program to "exit(0)" instead
> of "return 0", and valgrind now reports "unreachable" instead
> of "definite lost". Does it mean that I should always use "exit"
> to terminate programs? I thought that in main(), both "return" and
> "exit" achieve the same effect.
Both are fine - the difference is that return exits main, unwinding
the stack into the caller of main and in doing so valgrind will mark
that stack frame as invalid. Eventually the run time system (the
function which wraps main) will call the exit system call and the
process will die.
When you call exit() you leave the main() frame on the stack and
enter the exit() function which never returns - instead it calls
the exit system call and the process dies.
That is why your variable (which is part of main's stack frame) is
invalid in one case and valid in the other.
Tom
--
Tom Hughes (to...@co...)
http://www.compton.nu/
|