首页 > 解决方案 > 我无法在我的 C 程序中清除此内存泄漏

问题描述

所以在我的程序中,我被告知我有以下内存泄漏:

从以下位置分配的 6 个对象中直接泄漏 144 字节:
#0 0x7f14547bbb40 in __interceptor_malloc
#1 0x563579804cfa in getRow /home/runner/WORKING-PA2/main.c:38
#2 0x563579806040 in invert /home/runner /WORKING-PA2/main.c:162
#3 0x5635798072ab in main /home/runner/WORKING-PA2/main.c:261
#4 0x7f14535ecbf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+ 0x21bf6)

以下代码来自我的 main 函数的末尾,我试图释放我分配的所有内容。

double ** Xtranspose = transposeMatrix(X, numHouses, numAttributes+1);
double**  Xmult = multiply(Xtranspose,numAttributes+1,numHouses,X, numHouses, numAttributes+1);
double ** invertMatrix = invert(Xmult, numAttributes+1);
double**  invertMult = multiply(invertMatrix, numAttributes+1, numAttributes+1,Xtranspose,numAttributes+1,numHouses);
double**  W = multiply(invertMult, numAttributes+1,numHouses,Y, numHouses, 1);
double**  Yprime = multiply(Xprime, numHouses2, numAttributes2+1,W, numAttributes+1,1);
printMatrix(Yprime,numHouses2, 1);
    
freeMatrix(X, numHouses, numAttributes+1);
freeMatrix(Y,numHouses, 1);
freeMatrix(Xprime, numHouses2, numAttributes2+1);
freeMatrix(Xtranspose, numAttributes+1, numHouses);
freeMatrix(Xmult, numAttributes+1, numAttributes+1);
freeMatrix(invertMatrix, numAttributes+1,numAttributes+1);
freeMatrix(invertMult, numAttributes+1,numHouses);
freeMatrix(W, numAttributes+1,1);
freeMatrix(Yprime,numHouses2, 1);

这是反转功能:

double ** invert(double **matrix, int size){
    double ** identity = allocateMatrix(size, size);
    for(int i =0;i<size;i++){
        for(int j =0;j<size;j++){
            if(i==j){
                identity[i][j] = 1.0;
            }
            else{
                identity[i][j] = 0.0;
            }
        }
    }
    
    for(int j =0;j<size;j++){
        double * currentRow = getRow(size, j,matrix);
        double * currentRow2 = getRow(size, j,identity);
    
        if(matrix[j][j]==0){
            printMatrix(matrix,size,size);
            int a = j+1;
            while(a<size){
                if(matrix[a][j]!=0){
                    break;
                }
                a++;
            }
    
            double * otherRow2 = getRow(size, a, identity);
            otherRow2 = multiplyRowByScalar(size, otherRow2, 1/matrix[a][j]);
            otherRow2 = addRows(size,currentRow2, otherRow2);
            identity = setRow(size, j, identity, otherRow2);
    
            double * otherRow = getRow(size, a, matrix);
            otherRow = multiplyRowByScalar(size, otherRow, 1/matrix[a][j]);
            otherRow = addRows(size,currentRow, otherRow);
            matrix = setRow(size, j, matrix, otherRow);
    
            free(otherRow);
            free(otherRow2);
        }
        else if(matrix[j][j]==1){
            continue;
        }
        else{
            currentRow2 = multiplyRowByScalar(size, currentRow2, 1/matrix[j][j]);
            identity = setRow(size, j, identity, currentRow2);
    
            currentRow = multiplyRowByScalar(size, currentRow, 1/matrix[j][j]);
            matrix = setRow(size, j, matrix, currentRow);
        }
    
        for(int i=0;i<size;i++){
            currentRow = getRow(size, i,matrix);
            currentRow2 = getRow(size, i,identity);
    
            if(i!=j){
                if(matrix[i][j]!=0){
                    double * pivotRow2 = getRow(size, j,identity);
                    pivotRow2 = multiplyRowByScalar(size, pivotRow2, -matrix[i][j]);
                    currentRow2 = addRows(size,pivotRow2, currentRow2);
                    identity = setRow(size, i, identity, currentRow2);
    
                    double * pivotRow = getRow(size, j,matrix);
    
                    pivotRow = multiplyRowByScalar(size, pivotRow, -matrix[i][j]);
    
                    currentRow = addRows(size,pivotRow, currentRow);
                    matrix = setRow(size, i, matrix, currentRow);
                }
            }
    
            free(currentRow);
            free(currentRow2);
        }
    }
    return identity;
}

这是 getRow 函数:

double *getRow(int size, int rowNum, double **matrix){
  double *row = malloc(sizeof(double*)*size);
  for(int i =0;i<size;i++){
    row[i] =matrix[rowNum][i];
  }
  return row;
}

main 中的数据没有从 getRow 和 invert 函数中释放出来的方式有问题,我很确定,但我不知道为什么它没有被释放。我已经尝试过抛出自由和自由循环,但字节仍然泄漏。

标签: cmemory-managementmemory-leakscompiler-errors

解决方案


for循环正在重新分配currentRowand currentRow2,但您从未释放过它们的旧值。所以添加

free(currentRow);
free(currentRow2);

在循环之前。

你也从来没有自由pivotRowpivotRow2。您需要在块的末尾释放它们if(matrix[i][j]!=0){


推荐阅读