C 在函数中分配并在main中释放的结构的Valgrind报告泄漏

C 在函数中分配并在main中释放的结构的Valgrind报告泄漏,c,memory-leaks,valgrind,C,Memory Leaks,Valgrind,我正在尝试一个简单的函数来理解使用C和Valgrind的分配/自由工作流。Valgrind的报告对我来说毫无意义 函数创建一个结构并返回指向堆中结构的指针 指针指向的结构在main中填充 内容已打印出来 调用以释放结构 valgrind在可执行文件上运行 这是我的密码: #include <string.h> /* linked list abstraction */ struct ll { void *data; struct ll *prev;

我正在尝试一个简单的函数来理解使用C和Valgrind的分配/自由工作流。Valgrind的报告对我来说毫无意义

  • 函数创建一个结构并返回指向堆中结构的指针
  • 指针指向的结构在main中填充
  • 内容已打印出来
  • 调用以释放结构
  • valgrind在可执行文件上运行
  • 这是我的密码:

    #include <string.h>
    
    /* linked list abstraction */
    struct ll {
            void *data;
            struct ll *prev;
            struct ll *next;
    };
    
    /* struct to hold the data */
    struct device {
            char *name;
            unsigned int major;
            unsigned int minor;
            struct ll sm; /* struct on stack */
    };
    
    /* error checked malloc */
    static void *
    ec_malloc(size_t size)
    {
            void *ptr = malloc(size);
            if (unlikely(ptr == NULL)) {
                    perror("Error: ");
                    exit(-1);
            } else {
                    return ptr;
            }
    }
    
    struct device *
    create_dev(char *, unsigned int, unsigned int);
    
    int
    main(int argc, char **argv)
    {
            struct device *dev = create_dev("Dev01", 1, 2);
            printf("device: %s:%u:%u\n", dev->name, dev->major, dev->minor);
            free(dev);
            return 0;
    }
    
    struct device *
    create_dev(char *name, unsigned int major, unsigned int minor)
    {
            struct device *dev = (struct device *)ec_malloc(sizeof(*dev));
            dev->name = (char *)ec_malloc(BUFSIZE);
            strcpy(dev->name, name);
            dev->major = major;
            dev->minor = minor;
            return dev;
    }
    

    我希望Valgrind报告没有内存泄漏。然而,这份报告让我感到惊讶。感谢您的帮助

    尝试释放
    dev->name
    首先…

    在释放
    dev
    之前,您需要释放
    dev->name

    free()
    释放先前通过调用
    calloc
    malloc
    realloc
    分配的内存


    因此,一旦指针不再使用,请确保对上述函数分配的每个指针调用
    free()

    非常感谢!在混乱中,我错过了显而易见的东西!
    
    $ valgrind --leak-check=full ./devices
    ==1100== Memcheck, a memory error detector
    ==1100== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==1100== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
    ==1100== Command: ./devices
    ==1100==
    device: Dev01:1:2
    ==1100==
    ==1100== HEAP SUMMARY:
    ==1100==     in use at exit: 15 bytes in 1 blocks
    ==1100==   total heap usage: 3 allocs, 2 frees, 1,079 bytes allocated
    ==1100==
    ==1100== 15 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==1100==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==1100==    by 0x108B7E: ec_malloc (devices.h:37)
    ==1100==    by 0x108C3D: create_dev (main.c:84)
    ==1100==    by 0x108BD3: main (main.c:35)
    ==1100==
    ==1100== LEAK SUMMARY:
    ==1100==    definitely lost: 15 bytes in 1 blocks
    ==1100==    indirectly lost: 0 bytes in 0 blocks
    ==1100==      possibly lost: 0 bytes in 0 blocks
    ==1100==    still reachable: 0 bytes in 0 blocks
    ==1100==         suppressed: 0 bytes in 0 blocks
    ==1100==
    ==1100== For counts of detected and suppressed errors, rerun with: -v
    ==1100== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)