I am trying to allocate a memory to node in linked list using malloc function. However, I am getting segmentation fault on malloc call. I am unable to understand report generated by valgrind.
==28861== total heap usage: 76 allocs, 73 frees, 14,544 bytes allocated
==28861==
==28861== 48 bytes in 1 blocks are still reachable in loss record 1 of 3
==28861== at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==28861== by 0x4027A2: create_server_entry_into_connection_list (all.c:734)
==28861== by 0x401BF8: server_call (all.c:410)
==28861== by 0x40103F: main (all.c:108)
==28861==
==28861== 568 bytes in 1 blocks are still reachable in loss record 2 of 3
==28861== at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==28861== by 0x3E3C260309: __fopen_internal (in /lib64/libc-2.5.so)
==28861== by 0x4012E2: myip (all.c:174)
==28861== by 0x400FE7: main (all.c:101)
==28861==
==28861== 1,024 bytes in 1 blocks are still reachable in loss record 3 of 3
==28861== at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==28861== by 0x4027B3: create_server_entry_into_connection_list (all.c:736)
==28861== by 0x401BF8: server_call (all.c:410)
==28861== by 0x40103F: main (all.c:108)
==28861==
==28861== LEAK SUMMARY:
==28861== definitely lost: 0 bytes in 0 blocks
==28861== indirectly lost: 0 bytes in 0 blocks
==28861== possibly lost: 0 bytes in 0 blocks
==28861== still reachable: 1,640 bytes in 3 blocks
==28861== suppressed: 0 bytes in 0 blocks
==28861==
==28861== For counts of detected and suppressed errors, rerun with: -v
==28861== Use --track-origins=yes to see where uninitialised values come from
==28861== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
Segmentation fault
Could anyone explain what can be reason behind such error.
The reason? There are lots of possible reasons.
Try using errno.h it may be able to determine some problems.
strerror(errno) then returns the error as a string instead of a number.
Start with the first error from the debug console.. look at it carefully.
total heap usage: 76 allocs, 73 frees, 14,544 bytes allocated
...I think you should restructure your code. This time pay more attention and test the program oftenly. 76 allocs, 73 frees. Only 14,544 bytes allocated?
Use tests such as printf("p1 : %p\np2 : %p", pointer1, pointer2); to investiage memory address. valgrind sometimes behaves weird. It is dependable.
If your code is too large.. maybe use hastebin.com to provide a common view of your code for us.
Don't typecast the pointer.
Usually still reachable memory points towards the memory allocated to a global pointer and not being freed upon program completion. This does not always point to a memory leak. You can read more about that here.
Related
Is there a way to let valgrind to report "where" a "definitely lost" happens?
What I want is not "where it's allocated", but "where that poor piece of memory got leaked".
For example, this piece of code has a "definitely lost" leak when f() returns:
#include <stdlib.h>
void f () {
void *ptr = malloc(42);
}
int main () {
f();
return 0;
}
But valgrind only reports the origin of allocation:
==9772== HEAP SUMMARY:
==9772== in use at exit: 42 bytes in 1 blocks
==9772== total heap usage: 1 allocs, 0 frees, 42 bytes allocated
==9772==
==9772== 42 bytes in 1 blocks are definitely lost in loss record 1 of 1
==9772== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9772== by 0x40053E: f (test.c:4)
==9772== by 0x400552: main (test.c:8)
==9772==
==9772== LEAK SUMMARY:
==9772== definitely lost: 42 bytes in 1 blocks
==9772== indirectly lost: 0 bytes in 0 blocks
==9772== possibly lost: 0 bytes in 0 blocks
==9772== still reachable: 0 bytes in 0 blocks
==9772== suppressed: 0 bytes in 0 blocks
==9772==
==9772== For counts of detected and suppressed errors, rerun with: -v
==9772== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
I'm using --leak-check=full and --track-origins=yes.
What I want is not "where it's allocated", but "where that poor piece
of memory got leaked".
No, Valgrind only reports where the memory was allocated, but not when how or why it was leaked. This behavior is documented in Memcheck manual:
If --leak-check=full is specified, Memcheck will give details for each
definitely lost or possibly lost block, including where it was
allocated. (Actually, it merges results for all blocks that have the
same leak kind and sufficiently similar stack traces into a single
"loss record". The --leak-resolution lets you control the meaning of
"sufficiently similar".) It cannot tell you when or how or why the
pointer to a leaked block was lost; you have to work that out for
yourself.
Also --track-origins has nothing to do with memory leaks. It is used for tracking the origin of uninitialised values.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
char *str = malloc(sizeof(char)*5);
str = strcpy(str, "test");
printf("%s\n", str);
free(str);
return 0;
}
When I use Valgrind on my Mac (OS X, 10.9.5) I get the following message:
==77215== HEAP SUMMARY:
==77215== in use at exit: 29,211 bytes in 374 blocks
==77215== total heap usage: 451 allocs, 77 frees, 35,160 bytes allocated
==77215==
==77215== 4,096 bytes in 1 blocks are still reachable in loss record 76 of 76
==77215== at 0x66CB: malloc (in /usr/local/Cellar/valgrind/3.10.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==77215== by 0x182855: __smakebuf (in /usr/lib/system/libsystem_c.dylib)
==77215== by 0x197217: __swsetup (in /usr/lib/system/libsystem_c.dylib)
==77215== by 0x1B0158: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==77215== by 0x1B06AF: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==77215== by 0x187B29: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==77215== by 0x18596F: printf (in /usr/lib/system/libsystem_c.dylib)
==77215== by 0x100000F2B: main (test.c:8)
==77215==
==77215== LEAK SUMMARY:
==77215== definitely lost: 0 bytes in 0 blocks
==77215== indirectly lost: 0 bytes in 0 blocks
==77215== possibly lost: 0 bytes in 0 blocks
==77215== still reachable: 4,096 bytes in 1 blocks
==77215== suppressed: 25,115 bytes in 373 blocks
==77215==
==77215== For counts of detected and suppressed errors, rerun with: -v
==77215== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15)
Does printf allocate memory by itself? If I remove the printf I only get the following:
==77237== HEAP SUMMARY:
==77237== in use at exit: 25,115 bytes in 373 blocks
==77237== total heap usage: 450 allocs, 77 frees, 31,064 bytes allocated
==77237==
==77237== LEAK SUMMARY:
==77237== definitely lost: 0 bytes in 0 blocks
==77237== indirectly lost: 0 bytes in 0 blocks
==77237== possibly lost: 0 bytes in 0 blocks
==77237== still reachable: 0 bytes in 0 blocks
==77237== suppressed: 25,115 bytes in 373 blocks
==77237==
==77237== For counts of detected and suppressed errors, rerun with: -v
==77237== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15)
Where does the 373 blocks come from?
Until the Valgrind team prioritizes OS X, you can safely assume that it will not give correct results on Apple systems running OS X versions newer than 10.7.
On my Mavericks (10.9.5) machine, I still get the following warning from Valgrind (3.9.0)
WARNING: Support on MacOS 10.8/10.9 is experimental and mostly broken.
WARNING: Expect incorrect results, assertions and crashes.
WARNING: In particular, Memcheck on 32-bit programs will fail to
WARNING: detect any errors associated with heap-allocated data.
For what it's worth, Valgrind 3.10.0 shows no leaks on my Debian Jessie installation.
It isn't telling you that there is a leak:
==77215== LEAK SUMMARY:
==77215== definitely lost: 0 bytes in 0 blocks
==77215== indirectly lost: 0 bytes in 0 blocks
==77215== possibly lost: 0 bytes in 0 blocks
==77215== still reachable: 4,096 bytes in 1 blocks
==77215== suppressed: 25,115 bytes in 373 blocks
it's telling you that there is a block which is still reachable; whether or not there is a leak is dependent on your definition of 'leak'. What it does mean is that there is a pointer to the block somewhere.
Refer to Still Reachable Leak detected by Valgrind for more information.
I'm just learning C and don't understand why this little piece of code causes a memory leak:
mem_ptr=(char*)malloc(24);
if (mem_ptr == NULL){
exit(EXIT_FAILURE);
}
mem_ptr="TEST";
printf("%s\n",mem_ptr);
free(mem_ptr);
When I run it, Valgrind tells me:
==17900== Invalid free() / delete / delete[] / realloc()
==17900== at 0x4C2B75D: free (vg_replace_malloc.c:468)
==17900== by 0x40086F: main (main.c:69)
==17900== Address 0x40099f is not stack'd, malloc'd or (recently) free'd
==17900==
==17900==
==17900== HEAP SUMMARY:
==17900== in use at exit: 24 bytes in 1 blocks
==17900== total heap usage: 1 allocs, 1 frees, 24 bytes allocated
==17900==
==17900== LEAK SUMMARY:
==17900== definitely lost: 24 bytes in 1 blocks
==17900== indirectly lost: 0 bytes in 0 blocks
==17900== possibly lost: 0 bytes in 0 blocks
==17900== still reachable: 0 bytes in 0 blocks
==17900== suppressed: 0 bytes in 0 blocks
==17900== Rerun with --leak-check=full to see details of leaked memory
==17900==
==17900== For counts of detected and suppressed errors, rerun with: -v
==17900== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
You missed a strcpy():
mem_ptr=(char*)malloc(24);
mem_ptr="TEST";
This threw away the only pointer to the allocated space. When you tried to free "TEST" then valgrind complained that it wasn't allocated memory (and it was right). And you'd already leaked the 24 bytes you allocated.
Corrected:
mem_ptr = (char*)malloc(24);
if (mem_ptr == NULL)
exit(EXIT_FAILURE);
strcpy(mem_ptr, "TEST");
printf("%s\n", mem_ptr);
free(mem_ptr);
I was just testing out some c code I was writing and I found that I had a lot of memory leaks, so I wanted to see where they were coming from. All the code I had was:
int main() {
return 0;
}
Just a simple main that did nothing and I had all these memory leaks:
==38882== HEAP SUMMARY:
==38882== in use at exit: 58,678 bytes in 363 blocks
==38882== total heap usage: 514 allocs, 151 frees, 62,544 bytes allocated
==38882==
==38882== LEAK SUMMARY:
==38882== definitely lost: 8,624 bytes in 14 blocks
==38882== indirectly lost: 1,168 bytes in 5 blocks
==38882== possibly lost: 4,925 bytes in 68 blocks
==38882== still reachable: 43,961 bytes in 276 blocks
==38882== suppressed: 0 bytes in 0 blocks
==38882== Rerun with --leak-check=full to see details of leaked memory
==38882==
==38882== For counts of detected and suppressed errors, rerun with: -v
==38882== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
When I run this code on my university's computers I have no memory leaks. Is there something on my local machine that is causing this many memory leaks?
It is fine. Your program uses runtime library (i.e. libc or equivalent) and the library allocates some memory for own needs.
I get the same error on my Mac. It's either a valgrind problem or (more likely) a library problem on the Mac. I would suggest to use valgrind only on Linux.
I've used valgrind before and it has been very helpful. I recently setup a dev environment and starting using valgrind again. This time it finds NO lost memory! Even if I malloc some memory and then interrupt the program with CTRL-C, I get the dump below. Can someone explain what's going on?
Confused....
==2489== HEAP SUMMARY:
==2489== in use at exit: 314,145 bytes in 585 blocks
==2489== total heap usage: 1,410 allocs, 825 frees, 2,025,829 bytes allocated
==2489==
==2489== LEAK SUMMARY:
==2489== definitely lost: 0 bytes in 0 blocks
==2489== indirectly lost: 0 bytes in 0 blocks
==2489== possibly lost: 0 bytes in 0 blocks
==2489== still reachable: 314,145 bytes in 585 blocks
==2489== suppressed: 0 bytes in 0 blocks
==2489== Reachable blocks (those to which a pointer was found) are not shown.
==2489== To see them, rerun with: --leak-check=full --show-reachable=yes
==2489==
==2489== For counts of detected and suppressed errors, rerun with: -v
==2489== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)
If there is still a pointer to the malloced memory, it's not a leak. It's showing up as still reachable in the summary.
Memory that is not freeed is not necessarily leaked if it's still live, that is, there's still a pointer to it somewhere (globally, from the stack, or from registers.)