So in my program, I'm being told that I have a memory leak from the following:
Direct leak of 144 byte(s) in 6 object(s) allocated from:
#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)
The following code is from the end of my main function where I am trying to free up everything that I allocated.
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);
Here is the invert function:
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;
}
Here is the getRow function:
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;
}
Something is wrong with the way that the data in main isn't freeing from the getRow and invert function im pretty sure, but I cannot figure out why it isn't getting freed. I have tried throwing free and free loops all over, but the bytes remain leaked.
The for
loop is reassigning currentRow
and currentRow2
, but you never freed their old values. So add
free(currentRow);
free(currentRow2);
before the loop.
You also never free pivotRow
and pivotRow2
. You need to free them at the end of the if(matrix[i][j]!=0){
block.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.