valgrind |自制垃圾收集|丢失记录中肯定会丢失1个块中的字节

时间:2013-01-30 14:08:23

标签: garbage-collection malloc free valgrind

我写了一个非常复杂的软件(目前已经过了10年)。

您可能需要查看以下来源: http://savannah.nongnu.org/svn/?group=cybop

按照INSTALL文件的说明编译并运行。

我使用此命令行运行memcheck: christian @ deneb:/ home / project / cybop / examples $ valgrind --tool = memcheck --log-file = memcheck.log --leak-check = full --track-originins = yes ../src/controller/cyboi出口/ run.cybol

这是memcheck输出:

==28641== Memcheck, a memory error detector
==28641== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==28641== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==28641== Command: ../src/controller/cyboi exit/run.cybol
==28641== Parent PID: 22974
==28641== 
==28641== 
==28641== HEAP SUMMARY:
==28641==     in use at exit: 5,174 bytes in 407 blocks
==28641==   total heap usage: 636 allocs, 229 frees, 45,749 bytes allocated
==28641== 
==28641== 442 (72 direct, 370 indirect) bytes in 1 blocks are definitely lost in loss record 406 of 407
==28641==    at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==28641==    by 0x401EC0: allocate_array (array_allocator.c:116)
==28641==    by 0x40719C: allocate_part (part_allocator.c:50)
==28641==    by 0x42EE3E: deserialise_cybol_part_element_content (content_element_part_cybol_deserialiser.c:262)
==28641==    by 0x43098E: deserialise_cybol_part_element (element_part_cybol_deserialiser.c:74)
==28641==    by 0x4309FD: deserialise_cybol_part (part_cybol_deserialiser.c:79)
==28641==    by 0x42F230: deserialise_cybol_part_element_content (content_element_part_cybol_deserialiser.c:331)
==28641==    by 0x43098E: deserialise_cybol_part_element (element_part_cybol_deserialiser.c:74)
==28641==    by 0x4309FD: deserialise_cybol_part (part_cybol_deserialiser.c:79)
==28641==    by 0x42F230: deserialise_cybol_part_element_content (content_element_part_cybol_deserialiser.c:331)
==28641==    by 0x42F4CB: deserialise_cybol (cybol_deserialiser.c:196)
==28641==    by 0x42FD48: deserialise (deserialiser.c:229)
==28641== 
==28641== 4,732 (72 direct, 4,660 indirect) bytes in 1 blocks are definitely lost in loss record 407 of 407
==28641==    at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==28641==    by 0x401EC0: allocate_array (array_allocator.c:116)
==28641==    by 0x40719C: allocate_part (part_allocator.c:50)
==28641==    by 0x41F6EA: deserialise_xml_element (element_xml_deserialiser.c:61)
==28641==    by 0x41FA65: select_xml_element_content (element_content_xml_selector.c:144)
==28641==    by 0x41FAF3: deserialise_xml_element_content (element_content_xml_deserialiser.c:73)
==28641==    by 0x41FBA9: deserialise_xml (xml_deserialiser.c:66)
==28641==    by 0x42F3E1: deserialise_cybol (cybol_deserialiser.c:178)
==28641==    by 0x42FD48: deserialise (deserialiser.c:229)
==28641==    by 0x430088: receive_file (file_receiver.c:106)
==28641==    by 0x433D2F: initialise (initialiser.c:88)
==28641==    by 0x4344C3: manage (manager.c:320)
==28641== 
==28641== LEAK SUMMARY:
==28641==    definitely lost: 144 bytes in 2 blocks
==28641==    indirectly lost: 5,030 bytes in 405 blocks
==28641==      possibly lost: 0 bytes in 0 blocks
==28641==    still reachable: 0 bytes in 0 blocks
==28641==         suppressed: 0 bytes in 0 blocks
==28641== 
==28641== For counts of detected and suppressed errors, rerun with: -v
==28641== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)

“绝对丢失”的错误声称并非所有内容都已正确释放。 我不要求你做Sisyphus工作并搜索我的所有源代码。 基本上只有一个“malloc”,所有功能都依赖于它。 数据结构(自上而下)如下:part - item - array - value, 一世。即一个部分引用九个项目,一个项目引用三个数组。

我自制的内置垃圾收集说一切都很好。 也许有人想看看?任何提示都表示赞赏。 文件“array_allocator.c”的源代码片段如下:

void allocate_array(void* p0, void* p1, void* p2) {

    if (p0 != *NULL_POINTER_STATE_CYBOI_MODEL) {

        void** a = (void**) p0;

        // The memory area.
        int ma = *NUMBER_0_INTEGER_STATE_CYBOI_MODEL;

        // Determine type (type) size.
        determine_size((void*) &ma, p2);

        // Calculate memory area.
        calculate_integer_multiply((void*) &ma, p1);

        // The temporary size_t variable.
        size_t tma = ma;

        // CAUTION! In order to always get a correct memory address returned
        // that may be used to store data, a potential zero size is changed
        // to a minimum size of one here.
        if (tma == *NUMBER_0_INTEGER_STATE_CYBOI_MODEL) {

            tma = *NUMBER_1_INTEGER_STATE_CYBOI_MODEL;

        } else if (tma < *NUMBER_0_INTEGER_STATE_CYBOI_MODEL) {

fwprintf(stdout, L"ERROR: Could not allocate array. The memory area to be allocated is negative: %i\n", *a);

            tma = *NUMBER_1_INTEGER_STATE_CYBOI_MODEL;
        }

        *a = malloc(tma);

        // Initialise array elements with null pointer.
        //
        // CAUTION! Initialising with zero values is essential, since
        // cyboi frequently tests variables for null pointer values.
        memset(*a, *NUMBER_0_INTEGER_STATE_CYBOI_MODEL, tma);

    } else {

        log_message_terminated((void*) ERROR_LEVEL_LOG_CYBOI_MODEL, (void*) L"Could not allocate array. The array is null.");
    }
}

1 个答案:

答案 0 :(得分:0)

这里提出这个问题是一个错误,因为它没有 触摸一般的编程,而不是项目特定的 的问题。

我终于找到了这个bug。这是一个逻辑错误。

在解除分配(自定义)“部分”结构时, 需要“模型”的“类型”。错误是这样的 我在实际的“模型”之前取消了一个部分的“类型”, 所以“模型”解除分配不能正常工作 和内存泄漏发生。

解决。不属于stackoverflow。只是为了关闭它。