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

时间:2013-11-27 23:12:22

标签: 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("%s\n", 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] = '\0';
            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)

1 个答案:

答案 0 :(得分:2)

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