After 3 iterations, this routine increases in runtime from 3 seconds to 6
seconds.  Eventually, memory runs out and a core dump occurs.  I am using
the same array reference for the return value for each call, so the problem
is not that I am getting numerous arrays in memory (at least not on
purpose).

The function takes an array of image data (typically 512 x 512 pixels x 3
samples per pixel = about 780,000 array elements), a channel (0=Red,
1=Green, and 2=Blue), and turns all pixels blue if the pixel intensity in
the desired channel was below threshold, returning a reference to a new
array holding the masked image data.

Using newRV_noinc instead of newRV_inc (as per the discussion in perlguts),
I get lots of "attempt to free unreferenced scalar..." errors.

Can someone help me identify the memory leak?

Thanks,
Eric

---8<-----------------------------------------------------

SV* tiffRGBThreshold(SV* svImage, int iChan, int iThresh)
{
  AV *avImage, *avMask;
  int i, iImageA;

  avMask = newAV();

  //Extract and dereference perl array reference to AV*
  avImage = (AV*)SvRV(svImage);

  // calculate image size
  // (Need to add some error trapping here)
  iImageA = av_len(avImage);
  av_extend(avMask, iImageA);

  //Now loop through and turn pixels below threshold blue
  for (i=0; i<(iImageA + 1) / 3; i++)
  {
      if (SvIV(*av_fetch(avImage, i*3+iChan, 0)) < iThresh) 
        {  
          av_push(avMask, newSViv(0) );
          av_push(avMask, newSViv(0) );
          av_push(avMask, newSViv(255) );
        } else { 
          av_push(avMask, (SV*)*av_fetch(avImage, i*3, 0) );
          av_push(avMask, (SV*)*av_fetch(avImage, i*3+1, 0) );
          av_push(avMask, (SV*)*av_fetch(avImage, i*3+2, 0) );
    }
  }
  
  //return reference to array holding mask image data
  return newRV_inc((SV*)avMask);
}

Reply via email to