munmap_chunk():无效指针。需要有关如何释放所有内存的帮助

问题描述 投票:0回答:1

我从下面的代码中收到 munmap_chunk(): invalidpointer 错误。我在 Stackoverflow 上找不到与我的代码类似的任何内容,所以我基本上不知道如何继续。 我想我知道为什么会出现这些错误。 据我了解,即使我由于代码释放了所有动态分配的内存,我也无法释放所有内存。 就我而言,问题似乎源于(?)将数据发送到 csv 文件,这显然会导致内存泄漏。

我尝试了不同的方法来解决这个问题,但我一直没能做到。

我应该如何更改我的代码以便能够取消分配所有内存,如果可能的话,也许可以展示一个简单的示例来说明如何在将来避免这些问题。

感谢所有帮助。

程式化代码

    #define RAWR      429000000
    #define RAWC      5
    #define SUBR      43200000
    #define SUBC      11
    #define PROR      10000
    #define PROC      7200
    
    void f1read(double *raw, char *filename);   //creates raw from csv file
    void f1print(double *raw);                  //prints file to screen

    void f2sample (double *raw, double *subset);  //creates a subset from raw
    void f2print (double *subset);                //print subset to screen
    void f2csv (double *subset, char *filename);  //print subset to csv
    
    void f3sampleN (double *sampleN, double *pointK, double *pointS, ); //evaluation
    void f3printK (double *pointK);             //print pointK to screen
    void f3printS (double *pointS);             //print pointS to screen
    void f3csvK (double *pointK, char *filename);      //print pointK to csv
    void f3csvS (double *pointS, char *filename);      //print pointS to csv
    ....


    int main(void) {     
       ...
    for (n = 0; n < 500; n++) {
       double *raw = calloc(RAWR * RAWC, sizeof(double));   
       double *subset = calloc(SUBR * SUBC, sizeof(double)); 
       double *pointK = calloc(PROR * PROC, sizeof(double));
       double *pointS = calloc(PROR * PROC, sizeof(double)); 
       ....     
       
       char *data1 = dataFile1[n];      // name of the file
       char *data2 = dataFile2[n];  
       ....        

       f1read(rawData, char data1);      //data1 = name of original csvfile
       f1print(rawData);                 //print rawData to screen

       f2sample(rawData, sampleData);    //build sampleData from rawData
       f2printSample(sampleData);        //print sampleData to screen
       f2csvSample(sampleData, data2);   //print sampleData to csv-file                                                          

       f3sampleN(sampleN,pointK, pointS);          
       f3printK(pointK);
       f3printS(pointS);    
       f3csvK (pointK, data3);   
       f3csvK (pointS, data4);   
       ....

       free(rawData);
       free(sampleData);
       free(pointK);
       free(pointS);
       ....
     }                                                         
c memory-management memory-leaks
1个回答
0
投票

我终于发现错误了。我试图释放未分配的内存,同时我没有释放已分配的内存。 愚蠢的错误。抱歉给您带来不便。

© www.soinside.com 2019 - 2024. All rights reserved.