C-strdup内存泄漏

标签 c memory-leaks

我这里有这段代码,当我运行它时它可以正常工作,但最后它会从 strdup 函数中引发内存泄漏。 可以请教一下吗,我已经无计可施了。

我的代码


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

void modify(const char *input, char *output) {
    int lng = strlen(input);
    for (int i = 0; i < lng - 1; ++i) {
        output[i] = input[i];
    }
}

typedef struct {
    int index;
    char *ticker;
    float start;
    float end;
    int volume;
} Akcie;

void akcie_init(Akcie *akcie, int index, char *ticker, float start, float end, int volume) {
    akcie->index = index;
    akcie->ticker = ticker;
    akcie->start = start;
    akcie->end = end;
    akcie->volume = volume;
}

int main(int argc, char **argv) {
    Akcie *akcie;
    char *com;
    if (argc == 3) {
        int cislo = atoi(argv[2]);
        akcie = (Akcie *) malloc(cislo * sizeof(Akcie));
        char buf[101];
        char lo[100];
        char *toto[5];
        printf("<html>\n"
               "<body>\n"
               "<div>\n"
               "</div>\n"
               "<ul>\n");
        for (int i = 0; i < cislo; ++i) {
            fgets(buf, sizeof(buf), stdin);
            modify(buf, lo);
            com = strdup(lo);
            int j = 0;
            char *colo = strtok(com, ", ");
            while (colo != NULL) {
                toto[j] = colo;
                colo = strtok(NULL, ", ");
                j++;
            }
            akcie_init(&akcie[i], akcie[i].index = atoi(toto[0]), akcie[i].ticker = toto[1],
                       akcie[i].start = atof(toto[2]), akcie[i].end = atof(toto[3]), akcie[i].volume = atoi(toto[4]));
        }
        for (int l = cislo - 1; l >= 0; l--) {
            printf("<li> Day: %d, ticker: %s start: %.2f, end: %.2f, volume: %d </li> \n", akcie[l].index,
                   akcie[l].ticker, akcie[l].start, akcie[l].end, akcie[l].volume);
        }
        free(com);
        free(akcie);
        printf("</ul>\n"
               "</body>\n"
               "</html>");

    } else {
        printf("Wrong parameters");
    }
}

输出 + 内存泄漏

└─$ ./main AMC 10 <test-ticker-404.stdin
<html>
<body>
<div>
</div>
<ul>
<li> Day: 5, ticker: META start: 458.51, end: 462.19, volume: 328371639 </li> 
<li> Day: 4, ticker: GOOGL start: 687.75, end: 690.60, volume: 84021759 </li> 
<li> Day: 4, ticker: META start: 462.58, end: 458.51, volume: 536291890 </li> 
<li> Day: 3, ticker: AAPL start: 408.75, end: 411.80, volume: 737451730 </li> 
<li> Day: 3, ticker: GOOGL start: 687.98, end: 687.75, volume: 70074900 </li> 
<li> Day: 3, ticker: META start: 461.47, end: 462.58, volume: 275445389 </li> 
<li> Day: 2, ticker: AAPL start: 408.15, end: 408.75, volume: 475850689 </li> 
<li> Day: 2, ticker: GOOGL start: 685.68, end: 687.98, volume: 91902769 </li> 
<li> Day: 2, ticker: META start: 458.37, end: 461.47, volume: 19824825 </li> 
<li> Day: 1, ticker: AAPL start: 403.28, end: 408.15, volume: 71579480 </li> 
</ul>
</body>

=================================================================
==5879==ERROR: LeakSanitizer: detected memory leaks
                                                                                                                                                             
Direct leak of 277 byte(s) in 9 object(s) allocated from:
    #0 0x7f4b1487077b in __interceptor_strdup ../../../../src/libsanitizer/asan/asan_interceptors.cpp:439                                                    
    #1 0x55dd86eca65b in main main.c:46
    #2 0x7f4b14429209  (/lib/x86_64-linux-gnu/libc.so.6+0x29209)

SUMMARY: AddressSanitizer: 277 byte(s) leaked in 9 allocation(s).

我想保存每个股票中的 com,然后最后删除股票中所有这些 com 指标。或者甚至完全不同,不在堆上分配内存,而是将这些名称以静态数组的形式直接存储在共享中。但我不知道该怎么做。谢谢您的解决方案

最佳答案

当您在内循环中调用 com = strdup(lo); 但在外循环中仅调用 free 时,这是一个非常明显的泄漏。

内存泄漏最基本的规则:malloc/strdup调用次数必须与free调用次数匹配。

关于C-strdup内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74488508/

相关文章:

python - 一旦没有线程请求它,垃圾收集锁

c - 如何制作动态分配的结构数组?

c++ - accept() 函数的问题 如果我有超过 MAX_INT 个客户怎么办?

C++ 使用 boost::ptr_vector 泄漏内存

javascript - 移动端js内存泄漏

c++ - 在 std::shared_ptr 上使用 .reset() 是否会删除所有实例

c++ - 通过智能指针查找谁创建了对象

c - 如何 fork 并创建执行相同任务的特定数量的 child ?

c - 如何在获取输入的同时在数字数组中创建实时动态内存分配?

c - spi驱动中的基本设备操作