在不中止核心的情况下释放C中的内存



我刚刚用C为我正在处理的一个项目写了这段代码,一切都正常工作,唯一的问题是我有内存泄漏。我对C还很陌生,我的自由尝试只是以核心被抛弃而告终。有人能告诉我在哪里自由并解释为什么吗?我以为你只是担心释放malloc/pointers/arrays/structs中的任何东西,但我所有释放这些东西的尝试都失败了。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <utime.h>
#include <limits.h>

int main(int argc, char **argv)
{
    int i, status = 0;
    struct stat statbuf;
    struct tm *info;
    char buf[80];
    time_t now = time (NULL);
    for (i = 1; i < argc; i++) {
    if (lstat(argv[i], &statbuf)) {
        perror(argv[i]);
        status = 1;
    } else {
        printf("%6o ", statbuf.st_mode);
            int size = statbuf.st_size;
            printf("%9d",  size);    
            info = localtime(&statbuf.st_mtime);
            if(&now-15552000 < &statbuf.st_mtime){
             strftime(buf, 80, "%b %e %R", info);
             printf(" %s ", buf );}
            else{
            strftime(buf, 80, "%b %e  %Y", info);
            printf(" %s ", buf );}
            if (argc == 1) {
            printf(".n");}
        else if (S_ISREG(statbuf.st_mode))
        printf("%sn", argv[i]);
            char *pathname = argv[i];
            char linkname[PATH_MAX + 1];
            ssize_t retval = readlink (pathname, linkname, sizeof linkname);
            if (retval >= 0) {
            linkname[retval < PATH_MAX + 1 ? retval : PATH_MAX] = '';
            printf ("%s -> "%s"n", pathname, linkname);
      }     
    }
    }
    return(status);
}

我运行Valgrind来检查内存泄漏,这就是我得到的信息。对不起,我知道有点长:

> ==7314== Memcheck, a memory error detector
> ==7314== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
> ==7314== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
> ==7314== Command: 18stat.c
> ==7314==  18stat.c: line 14: syntax error near unexpected token `(' 18stat.c: line 14: `int main(int argc, char **argv)'
> ==7314== 
> ==7314== HEAP SUMMARY:
> ==7314==     in use at exit: 30,109 bytes in 664 blocks
> ==7314==   total heap usage: 766 allocs, 102 frees, 42,054 bytes allocated
> ==7314== 
> ==7314== 108 (32 direct, 76 indirect) bytes in 1 blocks are definitely lost in loss record 195 of 214
> ==7314==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
> ==7314==    by 0x465EA2: xmalloc (in /bin/bash)
> ==7314==    by 0x428D6A: make_bare_simple_command (in /bin/bash)
> ==7314==    by 0x42943D: make_simple_command (in /bin/bash)
> ==7314==    by 0x4261E7: yyparse (in /bin/bash)
> ==7314==    by 0x41D2E9: parse_command (in /bin/bash)
> ==7314==    by 0x41D3B5: read_command (in /bin/bash)
> ==7314==    by 0x41D607: reader_loop (in /bin/bash)
> ==7314==    by 0x41CCF8: main (in /bin/bash)
> ==7314== 
> ==7314== LEAK SUMMARY:
> ==7314==    definitely lost: 32 bytes in 1 blocks
> ==7314==    indirectly lost: 76 bytes in 5 blocks
> ==7314==      possibly lost: 0 bytes in 0 blocks
> ==7314==    still reachable: 30,001 bytes in 658 blocks
> ==7314==         suppressed: 0 bytes in 0 blocks
> ==7314== Reachable blocks (those to which a pointer was found) are not shown.
> ==7314== To see them, rerun with: --leak-check=full --show-reachable=yes
> ==7314== 
> ==7314== For counts of detected and suppressed errors, rerun with: -v
> ==7314== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)

确保在二进制文件上运行valgrind,而不是在源代码上。

最新更新