Please take as an example below. It seems there is no memory leak but why total heap usage and bytes allocated keeps increasing? If I remove first part(test1), valgrind result does not show any increasing heap and always total heap usage: 1 allocs, 1 frees, 568 bytes allocated
code example:
int main(void)
{
while(1)
{
FILE *test1;
test1 = fopen("test1", "w");
fprintf(test1, "something\n");
fclose(test1);
pid_t childPid;
childPid = fork();
if(childPid == 0)
{
int j;
FILE *test2;
test2 = fopen("test2", "w");
for(j = 0; j < 4; j++)
{
fprintf(test2, "something\n");
}
fclose(test2);
exit(0);
}
else
{
int returnStatus;
waitpid(childPid, &returnStatus, 0);
}
sleep(2);
}
return 0;
}
valgrind result:
==6314== Memcheck, a memory error detector
==6314== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6314== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==6314== Command: ./test
==6314== Parent PID: 6313
==6314==
==6315==
==6315== HEAP SUMMARY:
==6315== in use at exit: 0 bytes in 0 blocks
==6315== total heap usage: 2 allocs, 2 frees, 1,136 bytes allocated
==6315==
==6315== All heap blocks were freed -- no leaks are possible
==6315==
==6315== For counts of detected and suppressed errors, rerun with: -v
==6315== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==6317==
==6317== HEAP SUMMARY:
==6317== in use at exit: 0 bytes in 0 blocks
==6317== total heap usage: 3 allocs, 3 frees, 1,704 bytes allocated
==6317==
==6317== All heap blocks were freed -- no leaks are possible
==6317==
==6317== For counts of detected and suppressed errors, rerun with: -v
==6317== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==6319==
==6319== HEAP SUMMARY:
==6319== in use at exit: 0 bytes in 0 blocks
==6319== total heap usage: 4 allocs, 4 frees, 2,272 bytes allocated
==6319==
==6319== All heap blocks were freed -- no leaks are possible
==6319==
==6319== For counts of detected and suppressed errors, rerun with: -v
==6319== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)