【问题标题】:Valgrind - 2,064 bytes in 1 blocks are possibly lost in loss record 57 of 64Valgrind - 1 个块中的 2,064 字节可能在 64 的丢失记录 57 中丢失
【发布时间】:2016-06-30 16:10:05
【问题描述】:

昨天我问了一个问题,以便您可以帮助我调试一个程序,该程序基本上包括从标准输入中读取可变大小的行。过了一会儿,我以为我已经解决了所有问题,但是今天,valgrind 仍然在抱怨。

我编译了我的程序:

gcc -g -o find_all_words find_all_words.c

然后我使用 valgrind 如下:

valgrind --leak-check=full --show-leak-kinds=all ./find_all_words hello world

它还指向我代码的 while 循环中的另一部分:

==15333== Memcheck, a memory error detector
==15333== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15333== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==15333== Command: ./find_all_words hello world
==15333== 
Enter your favourite citation: hello world, how are you today?
line = hello world, how are you today?
Input arguments:
1 = hello
2 = world
==15333== 
==15333== HEAP SUMMARY:
==15333==     in use at exit: 30,666 bytes in 189 blocks
==15333==   total heap usage: 276 allocs, 87 frees, 36,976 bytes allocated
==15333== 
==15333== 2,064 bytes in 1 blocks are possibly lost in loss record 57 of 64
==15333==    at 0x100009104: malloc_zone_malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==15333==    by 0x1004F4EFD: _objc_copyClassNamesForImage (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004E8182: protocols() (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004E8093: readClass(objc_class*, bool, bool) (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004E5C13: gc_init (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004ED24E: objc_initializeClassPair_internal(objc_class*, char const*, objc_class*, objc_class*) (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004FA132: layout_string_create (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004E883C: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004E8300: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004E82E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004E82E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==15333==    by 0x1004E82E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==15333== 
==15333== 4,096 bytes in 1 blocks are still reachable in loss record 63 of 64
==15333==    at 0x100008E3B: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==15333==    by 0x1001E2AF5: __smakebuf (in /usr/lib/system/libsystem_c.dylib)
==15333==    by 0x1001E66D0: __srefill0 (in /usr/lib/system/libsystem_c.dylib)
==15333==    by 0x1001E67B8: __srefill (in /usr/lib/system/libsystem_c.dylib)
==15333==    by 0x1001DFA66: fgets (in /usr/lib/system/libsystem_c.dylib)
==15333==    by 0x100000A73: readline (find_all_words.c:26)
==15333==    by 0x100000E0D: main (find_all_words.c:94)
==15333== 
==15333== LEAK SUMMARY:
==15333==    definitely lost: 0 bytes in 0 blocks
==15333==    indirectly lost: 0 bytes in 0 blocks
==15333==      possibly lost: 2,064 bytes in 1 blocks
==15333==    still reachable: 4,096 bytes in 1 blocks
==15333==         suppressed: 24,506 bytes in 187 blocks
==15333== 
==15333== For counts of detected and suppressed errors, rerun with: -v
==15333== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 17)

这是我的程序:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/**
* Gets and a variable-size line from the standard input.
*/
char * readline(){

    size_t n = 10;
    char* final = calloc(n, sizeof(char));
    final[0] = '\0';
    char* tmp; // used for allocating memory temporarily

    // constant buffer size used to store the read characters
    // before storing them in the final buffer
    char buf[10];

    while(fgets(buf, sizeof buf, stdin) != NULL) {

        if(buf[strlen(buf) - 1] == '\n') {

            if(strlen(buf) > 1) {

                if((n - strlen(final)) < (strlen(buf) + 1)) {
                    // -1 because buf contains also \n at the end
                    n = strlen(final) + strlen(buf);
                    tmp = calloc(n, sizeof(char));

                    for(int i=0; i <= strlen(final); ++i)
                        tmp[i] = final[i];

                    free(final);

                } else {
                    tmp = final;
                }

                int i, j;
                for(i = strlen(tmp), j = 0; j <= (strlen(buf) - 2); ++i, ++j)
                    tmp[i] = buf[j];

                tmp[i] = '\0';
                final = tmp;
                tmp = NULL;
            }

            break;

        } else { // no newline inserted at the end

            if((n - strlen(final)) < (strlen(buf) + 1)) {
                n *= 2;
                tmp = calloc(n, sizeof(char));

                for(int i = 0; i <= strlen(final); ++i)
                    tmp[i] = final[i];

                free(final);

            } else {
                tmp = final;
            }       

            // Starts inserting from the '\0' char
            // Insert also the '\0' at the end
            for(int i = strlen(tmp), j = 0; j <= 9; ++i, ++j)
                tmp[i] = buf[j];

            final = tmp;
            tmp = NULL;
        }
    }

    return final;
}


int main(int argc, char *argv[]){
    if(argc < 2){
        fprintf(stderr, "usage: at least one string as command-line argument.\n");
        exit(1);
    } else {

        printf("Enter your favourite citation: ");

        char *line = readline();

        printf("line = %s\n", line);

        printf("Input arguments:\n");

        for(int i=1; i < argc; ++i)
            printf("%i = %s\n", i, argv[i]);

        free(line);
    }



    return 0;
}

感谢您的帮助!

【问题讨论】:

  • 奇数。我以与您相同的方式测试了您的代码(添加了必要的包含)并且 valgrind 没有抱怨。第 26 行是什么?
  • @JulienLopez 这是while循环...
  • 那你能把你的完整程序贴出来吗?根据您的 valgrind 输出,缺少几行。
  • @JulienLopez 这是我编译它的程序的所有代码,除了导入之外......我实际上还有另一个函数,但在我编译之前我已经从代码中删除了它。 ..它实际上并没有引起问题,因为即使我保留它,我也会收到明显相同的错误,也就是说,无论如何,这段代码有问题或者 valgrind 疯了......
  • 谢谢。好的,所以它不是代码。你在 OS X 上吗?

标签: c macos gcc memory-leaks valgrind


【解决方案1】:

看看这里:Possible Memory Leak Valgrind in OSX El Capitan 和这里:Valgrind Errors on Mac OS X for printf a double

你可以放心地忽略 valgrind 中的这些错误。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2011-04-05
    • 1970-01-01
    相关资源
    最近更新 更多