Valgrind奇怪的内存泄漏

时间:2020-10-20 12:55:34

标签: c macos valgrind clion

当我在小程序上启动valgrind时,出现了一个我不理解的非常奇怪的内存泄漏。报告中没有提到我的编码文件。

这是报告:

==6063==     in use at exit: 17,759 bytes in 159 blocks
==6063==   total heap usage: 178 allocs, 19 frees, 26,635 bytes allocated
==6063== 
==6063== 2,048 bytes in 1 blocks are definitely lost in loss record 34 of 37
==6063==    at 0x100123545: malloc (in /usr/local/Cellar/valgrind/HEAD-6049595/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==6063==    by 0x100701E95: class_setWeakIvarLayout (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x100701E26: class_setWeakIvarLayout (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x100705BF9: _objc_atfork_parent (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x1006F3742: property_copyAttributeList (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x1006E91E7: NXMapRemove (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x1006FBBD2: attachCategories(objc_class*, locstamped_category_t const*, unsigned int, int) (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x1006FB6C6: objc_class::mangledName() (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x1006FB69F: objc_class::mangledName() (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x1006FB69F: objc_class::mangledName() (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x1006FB69F: objc_class::mangledName() (in /usr/lib/libobjc.A.dylib)
==6063==    by 0x1006E7406: objc_opt::objc_stringhash_t::getIndex(char const*) const (in /usr/lib/libobjc.A.dylib)
==6063== 
==6063== LEAK SUMMARY:
==6063==    definitely lost: 2,048 bytes in 1 blocks
==6063==    indirectly lost: 0 bytes in 0 blocks
==6063==      possibly lost: 0 bytes in 0 blocks
==6063==    still reachable: 0 bytes in 0 blocks
==6063==         suppressed: 15,711 bytes in 158 blocks
==6063== 
==6063== For lists of detected and suppressed errors, rerun with: -s
==6063== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 7 from 7)

这些是我的项目文件:enter image description here

这是我的主要内容:

int main() {
    char** tab = allouer_tab_2D(3,4);
    afficher_tab_2D(tab, 3, 4);
    desallouer_tab_2D(tab, 3);
    int col = 0;
    int lig = 0;
    taille_fichier("../tabCaracteres.txt",&lig,&col);
    printf("Lignes : %d, Colonnes : %d", lig, col);
    return 0;
}

这是我的fonctions_fichiers.c(对不起法语):

#include "fonctions_fichiers.h"
#include <stdlib.h>
#include <stdio.h>

char** allouer_tab_2D(int n, int m){
    char** tab = malloc(n * sizeof(char*));
    for (int i = 0; i < n; i++){
        tab[i] = malloc(m * sizeof(char));
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            tab[i][j] = 'a';
        }
    }
    return tab;
}

void desallouer_tab_2D(char** tab, int n){
    for (int i = 0; i < n; ++i) {
        free(tab[i]);
    }
    free(tab);
}

void afficher_tab_2D(char** tab, int n, int m){
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            printf("%c", tab[i][j]);
        }
        printf("\n");
    }
}

void taille_fichier(const char* nomFichier, int* nbLig, int* nbCol){
    FILE* file = NULL;
    file = fopen(nomFichier,"r");
    int charac = 0;
    if(file != NULL){
        do {
            charac = getc(file);
            *nbCol += 1;
            if (charac == '\n') {
                *nbLig += 1;
            }
        } while (charac != EOF);
        fclose(file);
    }

}

tabCaracteres.txt:
aaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbb
ccccccccccc

0 个答案:

没有答案