1

Gmorning SO-

valgrind says:

==9735== 24,976 bytes in 446 blocks are definitely lost in loss record 9 of 9
==9735==    at 0x100012362: malloc (vg_replace_malloc.c:266)
==9735==    by 0x1000016F2: interpolate (in ./a.out)
==9735==    by 0x100000CFA: main (in ./a.out)

Yikes. Here's my interpolate function:

void interpolate(PDouble* evaluated, int doubleCount){
int i, j, k;
int boxCount = 0;
int frameCount = 0;

for(i=0; i<doubleCount; i++){
  boxCount = evaluated[i]->first->numBoxes;
  frameCount = evaluated[i]->gap;
  evaluated[i]->changeMatrix = (int***)malloc(boxCount*sizeof(int**));

for(j=0; j < boxCount; j++){
  evaluated[i]->changeMatrix[j] = (int **)malloc(ATTR * sizeof(int*));

  for(k=0; k < ATTR; k++){
    evaluated[i]->changeMatrix[j][k] = (int*)malloc(sizeof(int)*frameCount);
    if(evaluated[i]->differenceMatrix[j][k] > 200 ||
       evaluated[i]->differenceMatrix[j][k] < -200){
      generateRotationSequence(evaluated[i]->changeMatrix[j][k],
                               evaluated[i]->first->boxes[j]->o,
                               evaluated[i]->second->boxes[j]->o,
                               frameCount);
    }
    else{
      evaluated[i]->changeMatrix[j][k] = (int*)malloc(sizeof(int)*frameCount);
      generateSequence(evaluated[i]->changeMatrix[j][k], 
                       evaluated[i]->differenceMatrix[j][k], frameCount);
    }
   }
  }
 }
}

But I have the C functions that free all the pointers for PDoubles. I can provide the code if needed, but is there anything glaring about this that would make it leak, or make pointers dangle?

Edit: Realized I was using a strange term in description. Also this time im adding my destructors

void killDouble(PDouble marked){
int i, j, k;
int gap = marked->gap;
for(i=0; i < marked->first->numBoxes; i++){
  for(j=0; j < ATTR; j++){
    free(marked->changeMatrix[i][j]);
  }
  free(marked->changeMatrix[i]);
  free(marked->differenceMatrix[i]);
}
for(i=0; i < gap; i++){
  killFrame(marked->intFrames[i]);
}
killFrame(marked->first);
killFrame(marked->second);
free(marked->changeMatrix);
free(marked->differenceMatrix);
free(marked->intFrames);
free(marked);
}

2 Answers 2

5

You allocate evaluated[i]->changeMatrix[j][k]

evaluated[i]->changeMatrix[j][k] = (int*)malloc(sizeof(int)*frameCount);
if(evaluated[i]->differenceMatrix[j][k] > 200 ||
   evaluated[i]->differenceMatrix[j][k] < -200){
  generateRotationSequence(evaluated[i]->changeMatrix[j][k],
                           evaluated[i]->first->boxes[j]->o,
                           evaluated[i]->second->boxes[j]->o,
                           frameCount);
}
else{
  evaluated[i]->changeMatrix[j][k] = (int*)malloc(sizeof(int)*frameCount);

unconditionally and then again without freeing if the condition is not satisfied. That's your leak.

Since both allocations are exactly the same, you should remove the one in the else block, it is pointless.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I think I was leaving off something critical. I just added the function I call later on every "PDouble" in the program. Wouldn't that free it?
No, when you call it, the pointer obtained from the first allocation is already lost. Since the allocation is exactly the same as the one before the if, remove it. Or just put a free(evaluated[i]->changeMatrix[j][k]); at the start of the else block.
Got it. Didn't even realize I was mallocing it out twice. Thank you!
2
evaluated[i]->changeMatrix[j][k] = (int*)malloc(sizeof(int)*frameCount);

This is the leak. You allocate memory, but never free

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.