I'm working on a system programming project that requires forking N process. The issue is I'm keeping the PID's in a dynamicly allocated array and valgrind with "--leak-check=full --show-leak-kinds=all" flags shows this PID array pointer as possible leak.
pid_t *pid;
//...globals & prototypes
int main(int argc, char *argv[])
{
//... Input parsing
// Create actor process=========================================
pid = malloc((_N + _V + _C + 1) * sizeof(pid_t *)); // LINE VALGRIND IS POINTING STILL REACHABLE
for (int i = 0; i < _N + _V + _C + 1; i++)
{
pid[i] = fork();
if (pid[i] == 0)
break;
}
// ======================================== Create actor process
// Parent process ====================================================
if (parent_pid == getpid())
{
// Wait for all the childeren=====================================
for (int i = 0; i < _N + _V + _C + 1 || exit_requested != 0; i++)
{
int status;
if (waitpid(pid[i], &status, 0) == -1)
{
errExit("waitpid");
}
}
// =====================================Wait for all the childeren
// Free resources
free(pid);
//.. destroy semaphores
shm_unlink(SHARED_LINK);
}
// Child processes ===================================================
else
{
for (int i = 0; i < _N + _V + _C + 1; i++)
{
if (i >= 0 && i < _N && pid[i] == 0)
{
producer(_I, shared_data, i);
}
else if (i >= _N && i < _N + _V && pid[i] == 0)
{
mid_consumer(shared_data, i - _N);
}
else if (i >= _N + _V && i < _N + _V + _C && pid[i] == 0)
{
end_consumer(shared_data, i - _N - _V);
}
}
}
// ===================================================================
return 0;
}
Here is the valgrind output
==8056== Memcheck, a memory error detector
==8056== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8056== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==8056== Command: ./program -n 2 -v 2 -c 4 -b 81 -t 2 -i cold_storage.txt
==8056== Parent PID: 8055
==8056==
==8061==
==8061== HEAP SUMMARY:
==8061== in use at exit: 72 bytes in 1 blocks
==8061== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8061==
==8061== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8061== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8061== by 0x10925D: main (main.c:169)
==8061==
==8061== LEAK SUMMARY:
==8061== definitely lost: 0 bytes in 0 blocks
==8061== indirectly lost: 0 bytes in 0 blocks
==8061== possibly lost: 0 bytes in 0 blocks
==8061== still reachable: 72 bytes in 1 blocks
==8061== suppressed: 0 bytes in 0 blocks
==8061==
==8061== For counts of detected and suppressed errors, rerun with: -v
==8061== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8059==
==8059== HEAP SUMMARY:
==8059== in use at exit: 72 bytes in 1 blocks
==8059== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8059==
==8057==
==8062==
==8057== HEAP SUMMARY:
==8057== in use at exit: 72 bytes in 1 blocks
==8057== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8057==
==8062== HEAP SUMMARY:
==8062== in use at exit: 72 bytes in 1 blocks
==8062== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8062==
==8059== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8059== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8059== by 0x10925D: main (main.c:169)
==8059==
==8059== LEAK SUMMARY:
==8059== definitely lost: 0 bytes in 0 blocks
==8059== indirectly lost: 0 bytes in 0 blocks
==8059== possibly lost: 0 bytes in 0 blocks
==8059== still reachable: 72 bytes in 1 blocks
==8059== suppressed: 0 bytes in 0 blocks
==8059==
==8059== For counts of detected and suppressed errors, rerun with: -v
==8059== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8062== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8057== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8062== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8057== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8062== by 0x10925D: main (main.c:169)
==8062==
==8057== by 0x10925D: main (main.c:169)
==8062== LEAK SUMMARY:
==8057==
==8062== definitely lost: 0 bytes in 0 blocks
==8057== LEAK SUMMARY:
==8062== indirectly lost: 0 bytes in 0 blocks
==8057== definitely lost: 0 bytes in 0 blocks
==8062== possibly lost: 0 bytes in 0 blocks
==8057== indirectly lost: 0 bytes in 0 blocks
==8062== still reachable: 72 bytes in 1 blocks
==8057== possibly lost: 0 bytes in 0 blocks
==8062== suppressed: 0 bytes in 0 blocks
==8057== still reachable: 72 bytes in 1 blocks
==8062==
==8057== suppressed: 0 bytes in 0 blocks
==8062== For counts of detected and suppressed errors, rerun with: -v
==8057==
==8062== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8057== For counts of detected and suppressed errors, rerun with: -v
==8057== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8063==
==8063== HEAP SUMMARY:
==8063== in use at exit: 72 bytes in 1 blocks
==8063== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8063==
==8063== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8063== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8063== by 0x10925D: main (main.c:169)
==8063==
==8063== LEAK SUMMARY:
==8063== definitely lost: 0 bytes in 0 blocks
==8063== indirectly lost: 0 bytes in 0 blocks
==8063== possibly lost: 0 bytes in 0 blocks
==8063== still reachable: 72 bytes in 1 blocks
==8063== suppressed: 0 bytes in 0 blocks
==8063==
==8063== For counts of detected and suppressed errors, rerun with: -v
==8063== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8058==
==8058== HEAP SUMMARY:
==8058== in use at exit: 72 bytes in 1 blocks
==8058== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8058==
==8058== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8058== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8058== by 0x10925D: main (main.c:169)
==8058==
==8058== LEAK SUMMARY:
==8058== definitely lost: 0 bytes in 0 blocks
==8058== indirectly lost: 0 bytes in 0 blocks
==8058== possibly lost: 0 bytes in 0 blocks
==8058== still reachable: 72 bytes in 1 blocks
==8058== suppressed: 0 bytes in 0 blocks
==8058==
==8058== For counts of detected and suppressed errors, rerun with: -v
==8058== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8064==
==8064== HEAP SUMMARY:
==8064== in use at exit: 72 bytes in 1 blocks
==8064== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8064==
==8064== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8064== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8064== by 0x10925D: main (main.c:169)
==8064==
==8064== LEAK SUMMARY:
==8064== definitely lost: 0 bytes in 0 blocks
==8064== indirectly lost: 0 bytes in 0 blocks
==8064== possibly lost: 0 bytes in 0 blocks
==8064== still reachable: 72 bytes in 1 blocks
==8064== suppressed: 0 bytes in 0 blocks
==8064==
==8064== For counts of detected and suppressed errors, rerun with: -v
==8064== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8060==
==8060== HEAP SUMMARY:
==8060== in use at exit: 72 bytes in 1 blocks
==8060== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8060==
==8060== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8060== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8060== by 0x10925D: main (main.c:169)
==8060==
==8060== LEAK SUMMARY:
==8060== definitely lost: 0 bytes in 0 blocks
==8060== indirectly lost: 0 bytes in 0 blocks
==8060== possibly lost: 0 bytes in 0 blocks
==8060== still reachable: 72 bytes in 1 blocks
==8060== suppressed: 0 bytes in 0 blocks
==8060==
==8060== For counts of detected and suppressed errors, rerun with: -v
==8060== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8065==
==8065== HEAP SUMMARY:
==8065== in use at exit: 72 bytes in 1 blocks
==8065== total heap usage: 1 allocs, 0 frees, 72 bytes allocated
==8065==
==8065== 72 bytes in 1 blocks are still reachable in loss record 1 of 1
==8065== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8065== by 0x10925D: main (main.c:169)
==8065==
==8065== LEAK SUMMARY:
==8065== definitely lost: 0 bytes in 0 blocks
==8065== indirectly lost: 0 bytes in 0 blocks
==8065== possibly lost: 0 bytes in 0 blocks
==8065== still reachable: 72 bytes in 1 blocks
==8065== suppressed: 0 bytes in 0 blocks
==8065==
==8065== For counts of detected and suppressed errors, rerun with: -v
==8065== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==8056==
==8056== HEAP SUMMARY:
==8056== in use at exit: 0 bytes in 0 blocks
==8056== total heap usage: 1 allocs, 1 frees, 72 bytes allocated
==8056==
==8056== All heap blocks were freed -- no leaks are possible
==8056==
==8056== For counts of detected and suppressed errors, rerun with: -v
==8056== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
I tried freeing the PID array in the forked processes too but it still gives the same error.
This is not a complete answer, but it addresses some concerns.
First, the pid array memory block is being allocated with the wrong size based on a multiple of sizeof(pid_t *) when it should be the same multiple of sizeof(pid_t). That is a harmless waste of memory if sizeof(pid_t *) is greater than sizeof(pid_t), but would be a buffer overrun in the unlike case that sizeof(pid_t *) is less than sizeof(pid_t).
Second, the child processes do not free the pid array memory block.
Third, the child processes loop through all the elements of the pid array memory block looking for pid[i] == 0. I believe this is the child process trying to find its own index. However, the pid array memory block has not been fully initialized in the child process fork, so there could be more than one element with the value 0. Besides, there is no need for the child process to use the pid array at all if all it needs is its index number which it can determine at the time of the fork().
The version below fixes those issues, but I don't know if it fixes the Valgrind errors.
pid_t *pid;
//...globals & prototypes
int main(int argc, char *argv[])
{
int cid; // child index
//... Input parsing
// Create actor process=========================================
pid = malloc((_N + _V + _C + 1) * sizeof(pid_t));
for (cid = 0; xid < _N + _V + _C + 1; cid++)
{
pid[cid] = fork();
if (pid[cid] == 0)
{
free(pid);
break;
}
}
// ======================================== Create actor process
// Parent process ====================================================
if (parent_pid == getpid())
{
// Wait for all the childeren=====================================
for (int i = 0; i < _N + _V + _C + 1 || exit_requested != 0; i++)
{
int status;
if (waitpid(pid[i], &status, 0) == -1)
{
errExit("waitpid");
}
}
// =====================================Wait for all the childeren
// Free resources
free(pid);
//.. destroy semaphores
shm_unlink(SHARED_LINK);
}
// Child processes ===================================================
else
{
if (cid >= 0 && cid < _N)
{
producer(_I, shared_data, cid);
}
else if (cid >= _N && cid < _N + _V)
{
mid_consumer(shared_data, cid - _N);
}
else if (cid >= _N + _V && cid < _N + _V + _C)
{
end_consumer(shared_data, cid - _N - _V);
}
}
// ===================================================================
return 0;
}
Related
When I try recursively traverse directories in fork.Fork causing memory leak.İf I do just fork everythink okey but when I called function in child process I see memory leak on valgrind.I think ,I close every directory.But valgrind says opendir_tail.I tried write clodir in parent but then I take 1 allocated but 2 free error.Where is the problem ?
Here my code.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
int dfsdirectory (char *path){
DIR* dir;
struct dirent *dirEntry;
struct stat fileStat;
char _PathN[1000];
int totalSize=0;
if (!(dir = opendir(path))){
closedir(dir);
return -1;
}
while ( ((dirEntry=readdir(dir)) != 0) ) {
int dirSize=0;
if (strcmp(dirEntry->d_name, ".") == 0
|| strcmp(dirEntry->d_name, "..") == 0)
continue;
snprintf(_PathN, sizeof(_PathN), "%s/%s", path, dirEntry->d_name);
lstat (_PathN, &fileStat);
if (S_ISDIR(fileStat.st_mode)){
int PID = fork();
if(PID<0){
closedir(dir);
return -1;
}
if(PID == 0){
dfsdirectory(_PathN);
while ((closedir(dir) == -1) && (errno == EINTR));
exit(0);
}
else{
wait(NULL);
}
}
}
closedir(dir);
return totalSize;
}
int main() {
dfsdirectory("A");
}
And valgrind output
==4070== Memcheck, a memory error detector
==4070== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4070== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright
info
==4070== Command: ./main
==4070== Parent PID: 953
==4070==
==4072==
==4072== HEAP SUMMARY:
==4072== in use at exit: 32,816 bytes in 1 blocks
==4072== total heap usage: 3 allocs, 2 frees, 98,448 bytes allocated
==4072==
==4072== 32,816 bytes in 1 blocks are still reachable in loss record 1 of 1
==4072== at 0x4C2BBAF: malloc (vg_replace_malloc.c:299)
==4072== by 0x4EEB813: __alloc_dir (opendir.c:247)
==4072== by 0x4EEB902: opendir_tail (opendir.c:145)
==4072== by 0x108977: dfsdirectory (in /home/alex/Desktop/main)
==4072== by 0x108AE6: main (in /home/alex/Desktop/main)
==4072==
==4072== LEAK SUMMARY:
==4072== definitely lost: 0 bytes in 0 blocks
==4072== indirectly lost: 0 bytes in 0 blocks
==4072== possibly lost: 0 bytes in 0 blocks
==4072== still reachable: 32,816 bytes in 1 blocks
==4072== suppressed: 0 bytes in 0 blocks
==4072==
==4072== For counts of detected and suppressed errors, rerun with: -v
==4072== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==4071==
==4071== HEAP SUMMARY:
==4071== in use at exit: 0 bytes in 0 blocks
==4071== total heap usage: 2 allocs, 2 frees, 65,632 bytes allocated
==4071==
==4071== All heap blocks were freed -- no leaks are possible
==4071==
==4071== For counts of detected and suppressed errors, rerun with: -v
==4071== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==4075==
==4075== HEAP SUMMARY:
==4075== in use at exit: 65,632 bytes in 2 blocks
==4075== total heap usage: 4 allocs, 2 frees, 131,264 bytes allocated
==4075==
==4075== 32,816 bytes in 1 blocks are still reachable in loss record 1 of 2
==4075== at 0x4C2BBAF: malloc (vg_replace_malloc.c:299)
==4075== by 0x4EEB813: __alloc_dir (opendir.c:247)
==4075== by 0x4EEB902: opendir_tail (opendir.c:145)
==4075== by 0x108977: dfsdirectory (in /home/alex/Desktop/main)
==4075== by 0x108AE6: main (in /home/alex/Desktop/main)
==4075==
==4075== 32,816 bytes in 1 blocks are still reachable in loss record 2 of 2
==4075== at 0x4C2BBAF: malloc (vg_replace_malloc.c:299)
==4075== by 0x4EEB813: __alloc_dir (opendir.c:247)
==4075== by 0x4EEB902: opendir_tail (opendir.c:145)
==4075== by 0x108977: dfsdirectory (in /home/alex/Desktop/main)
==4075== by 0x108A78: dfsdirectory (in /home/alex/Desktop/main)
==4075== by 0x108AE6: main (in /home/alex/Desktop/main)
==4075==
==4075== LEAK SUMMARY:
==4075== definitely lost: 0 bytes in 0 blocks
==4075== indirectly lost: 0 bytes in 0 blocks
==4075== possibly lost: 0 bytes in 0 blocks
==4075== still reachable: 65,632 bytes in 2 blocks
==4075== suppressed: 0 bytes in 0 blocks
==4075==
==4075== For counts of detected and suppressed errors, rerun with: -v
==4075== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==4076==
==4076== HEAP SUMMARY:
==4076== in use at exit: 65,632 bytes in 2 blocks
==4076== total heap usage: 4 allocs, 2 frees, 131,264 bytes allocated
==4076==
==4076== 32,816 bytes in 1 blocks are still reachable in loss record 1 of 2
==4076== at 0x4C2BBAF: malloc (vg_replace_malloc.c:299)
==4076== by 0x4EEB813: __alloc_dir (opendir.c:247)
==4076== by 0x4EEB902: opendir_tail (opendir.c:145)
==4076== by 0x108977: dfsdirectory (in /home/alex/Desktop/main)
==4076== by 0x108AE6: main (in /home/alex/Desktop/main)
==4076==
==4076== 32,816 bytes in 1 blocks are still reachable in loss record 2 of 2
==4076== at 0x4C2BBAF: malloc (vg_replace_malloc.c:299)
==4076== by 0x4EEB813: __alloc_dir (opendir.c:247)
==4076== by 0x4EEB902: opendir_tail (opendir.c:145)
==4076== by 0x108977: dfsdirectory (in /home/alex/Desktop/main)
==4076== by 0x108A78: dfsdirectory (in /home/alex/Desktop/main)
==4076== by 0x108AE6: main (in /home/alex/Desktop/main)
==4076==
==4076== LEAK SUMMARY:
==4076== definitely lost: 0 bytes in 0 blocks
==4076== indirectly lost: 0 bytes in 0 blocks
==4076== possibly lost: 0 bytes in 0 blocks
==4076== still reachable: 65,632 bytes in 2 blocks
==4076== suppressed: 0 bytes in 0 blocks
==4076==
==4076== For counts of detected and suppressed errors, rerun with: -v
==4076== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==4074==
==4074== HEAP SUMMARY:
==4074== in use at exit: 32,816 bytes in 1 blocks
==4074== total heap usage: 3 allocs, 2 frees, 98,448 bytes allocated
==4074==
==4074== 32,816 bytes in 1 blocks are still reachable in loss record 1 of 1
==4074== at 0x4C2BBAF: malloc (vg_replace_malloc.c:299)
==4074== by 0x4EEB813: __alloc_dir (opendir.c:247)
==4074== by 0x4EEB902: opendir_tail (opendir.c:145)
==4074== by 0x108977: dfsdirectory (in /home/alex/Desktop/main)
==4074== by 0x108AE6: main (in /home/alex/Desktop/main)
==4074==
==4074== LEAK SUMMARY:
==4074== definitely lost: 0 bytes in 0 blocks
==4074== indirectly lost: 0 bytes in 0 blocks
==4074== possibly lost: 0 bytes in 0 blocks
==4074== still reachable: 32,816 bytes in 1 blocks
==4074== suppressed: 0 bytes in 0 blocks
==4074==
==4074== For counts of detected and suppressed errors, rerun with: -v
==4074== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==4073==
==4073== HEAP SUMMARY:
==4073== in use at exit: 0 bytes in 0 blocks
==4073== total heap usage: 2 allocs, 2 frees, 65,632 bytes allocated
==4073==
==4073== All heap blocks were freed -- no leaks are possible
==4073==
==4073== For counts of detected and suppressed errors, rerun with: -v
==4073== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==4070==
==4070== HEAP SUMMARY:
==4070== in use at exit: 0 bytes in 0 blocks
==4070== total heap usage: 1 allocs, 1 frees, 32,816 bytes allocated
==4070==
==4070== All heap blocks were freed -- no leaks are possible
==4070==
==4070== For counts of detected and suppressed errors, rerun with: -v
==4070== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
This memory leak is related to the recursion. You do not close the directory handles further up the stack:
==4075== by 0x4EEB902: opendir_tail (opendir.c:145)
==4075== by 0x108977: dfsdirectory (in /home/alex/Desktop/main)
==4075== by 0x108AE6: main (in /home/alex/Desktop/main)
==4075== by 0x4EEB902: opendir_tail (opendir.c:145)
==4075== by 0x108977: dfsdirectory (in /home/alex/Desktop/main)
==4075== by 0x108A78: dfsdirectory (in /home/alex/Desktop/main)
==4075== by 0x108AE6: main (in /home/alex/Desktop/main)
It is a leak of the still reachable kind, and these can be harmless, either because you call exit early (which applies here), or because they refer to data structures that are rooted in global variables. There is considerable disagreement among C developers whether these leaks need to be addressed, or whether it is acceptable to leave them in a program.
Please also note that the EINTR check for closedir is wrong with glibc. It introduces a double-free vulnerability in case of an EINTR failure.
I found the solution.Close the directory before recursive call and there is not any memory leak
Hi my Valgrind shows me memory error but I cant find it.
==18608== HEAP SUMMARY:
==18608== in use at exit: 88 bytes in 1 blocks
==18608== total heap usage: 4 allocs, 3 frees, 2,220 bytes allocated
==18608==
==18608== Searching for pointers to 1 not-freed blocks
==18608== Checked 53,708 bytes
==18608==
==18608== 88 bytes in 1 blocks are definitely lost in loss record 1 of 1
==18608== at 0x402D17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==18608== by 0x8
0485BF: pmalloc (in auf)
==18608== by 0x8048680: main (in auf)
==18608==
==18608== LEAK SUMMARY:
==18608== definitely lost: 88 bytes in 1 blocks
==18608== indirectly lost: 0 bytes in 0 blocks
==18608== possibly lost: 0 bytes in 0 blocks
==18608== still reachable: 0 bytes in 0 blocks
==18608== suppressed: 0 bytes in 0 blocks
==18608==
==18608== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==18608== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
You allocate two different int *s; you don't free the first resource but you free the second one without doing anything with it:
print_prim(pmalloc(n), laenge);
free(pmalloc(laenge));
To free the first resource properly, you would do something like this:
int *p = pmalloc(n);
print_prim(p, laenge);
free(p);
I'm running valgrind and it's telling me that I need to free something and I'm not sure where I would do that. It specifically points to this line "toInsert->value = linkedlist->copy(element);
Where would I run free and on what data?
"
void linkedlist_append(LinkedList *linkedlist, void *element) {
ListNode *toInsert = (ListNode*)malloc(sizeof(ListNode));
toInsert->value = linkedlist->copy(element);
toInsert->next = NULL;
ListNode *last = linkedlist->head;
if (last==NULL)
linkedlist->head = toInsert;
else{
while(last-> next !=NULL){
last = last->next;
}
last->next = toInsert;
}
linkedlist->size++;
}
Output from Valgrind:
> ^C==30515==
==30515== HEAP SUMMARY:
==30515== in use at exit: 287 bytes in 47 blocks
==30515== total heap usage: 95 allocs, 1,038 frees, 2,159 bytes allocated
==30515==
==30515== 247 bytes in 46 blocks are definitely lost in loss record 2 of 2
==30515== at 0x4C28C20: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30515== by 0x4011AC: string_copy (string_fns.c:27)
==30515== by 0x4012CE: linkedlist_append (linkedlist.c:87)
==30515== by 0x4017BD: add_file (tester.c:194)
==30515== by 0x400FE1: main (tester.c:136)
==30515==
==30515== LEAK SUMMARY:
==30515== definitely lost: 247 bytes in 46 blocks
==30515== indirectly lost: 0 bytes in 0 blocks
==30515== possibly lost: 0 bytes in 0 blocks
==30515== still reachable: 40 bytes in 1 blocks
==30515== suppressed: 0 bytes in 0 blocks
==30515== Reachable blocks (those to which a pointer was found) are not shown.
==30515== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==30515==
==30515== For counts of detected and suppressed errors, rerun with: -v
==30515== ERROR SUMMARY: 2200 errors from 10 contexts (suppressed: 0 from 0)
Valgrind is pointing you to the location of a malloc that is never freed. Before you terminate your program, you will need to iterate through the list an free all its elements.
#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 have the following code, and for some reason valgrind finds some memory leaks in the getlogin() function.
The code:
#include<unistd.h>
#include<stdio.h>
int main()
{
char *userName = getlogin();
printf("%s\n",userName);
return 0;
}
The valgrind command which I use:
valgrind --leak-check=full -v ./
The error I get:
HEAP SUMMARY:
==2405== in use at exit: 300 bytes in 11 blocks
==2405== total heap usage: 67 allocs, 56 frees, 9,106 bytes allocated
==2405==
==2405== Searching for pointers to 11 not-freed blocks
==2405== Checked 72,264 bytes
==2405==
==2405== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
==2405== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2405== by 0x4F37CD4: nss_parse_service_list (nsswitch.c:678)
==2405== by 0x4F38795: __nss_database_lookup (nsswitch.c:175)
==2405== by 0x55F6623: ???
==2405== by 0x4EF144C: getpwuid_r##GLIBC_2.2.5 (getXXbyYY_r.c:256)
==2405== by 0x4F145AE: __getlogin_r_loginuid (getlogin_r.c:68)
==2405== by 0x4F14304: getlogin (getlogin.c:35)
==2405== by 0x400550: main (tmp1.c:6)
==2405==
==2405== LEAK SUMMARY:
==2405== definitely lost: 60 bytes in 1 blocks
==2405== indirectly lost: 240 bytes in 10 blocks
==2405== possibly lost: 0 bytes in 0 blocks
==2405== still reachable: 0 bytes in 0 blocks
==2405== suppressed: 0 bytes in 0 blocks
==2405==
==2405== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
--2405--
--2405-- used_suppression: 2 dl-hack3-cond-1
==2405==
==2405== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
Why does it happen? And what can I do to free it? Is there any other option to get the user name? Thanks in advance.
With valgrind 3.10.0 I have no error
==27656==
==27656== HEAP SUMMARY:
==27656== in use at exit: 0 bytes in 0 blocks
==27656== total heap usage: 67 allocs, 67 frees, 9,101 bytes allocated
==27656==
==27656== All heap blocks were freed -- no leaks are possible
==27656==
==27656== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==27656== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)>
Try to upgrade it or call __libc_freeres() after getlogin()
But there is no error in your programme, this type of memory leak is acceptable.