Title: Attempt to free unreference scalar?

Dear perl-xs experts:

The following code is a stripped down version of an application I am writing to do some quantitative image analysis for our lab.  This version is spurious...it simply copies an array...but in reality, I want to do various complicated mathematical manipulations in the inline'd subroutine.

My problem is that the inline'd xsub (copyImage) works once, works a second time, but then upon being called the third time my screen fills with "Attempt to free unreferenced scalar" warnings.

Can anyone tell me why?

As this is my first post to this mailing list, gentle admonishments regarding sins of commission or omission are welcome also.

Thanks,
Eric

-----8<---code snippet follows--------------------------
#!/usr/bin/perl -w

use strict;
use vars qw(@image $image $size $copiedImage $x $prompt);

# for the purpose of this snippet, create an array
# of random pixel intensities equal in size to a 512x512 image
push @image, int(rand()*255) for (1..262144);

# now try to copy it via copyImage several times to invoke
# errors
for ($x=1; $x<=2; $x++)
{
  $image = \@image; # need a reference to pass to copyImage
  $copiedImage = copyImage($image);
  print "Copied image $x times (press enter to continue)\n";
  $prompt = (<STDIN>); # just so we can see what is happening
  $copiedImage = 0; # destroy reference to clear memory
}

exit;

use Inline C => <<'END_OF_C_CODE';
SV* copyImage(SV* rv_inputImage){

  AV* avPtr_filteredImage; // pointer to array holding copied image data
  AV* avPtr_inputImage; // pointer to array referenced by rv_inputImage
  double iterate; // counter for loop
  SV* pixel; // intensity of each pixel (0-255)
  SV* svPtr_arrayRef; // reference to copied array, to be returned

  // Convert the perl array ref to C array pointer by
  // extracting the reference value and casting it to an AV*
  avPtr_inputImage = (AV*)SvRV(rv_inputImage);
  avPtr_filteredImage = newAV();

  for (iterate=0; iterate<262144; iterate++)
  {
    av_push(avPtr_filteredImage, *av_fetch(avPtr_inputImage, iterate, 0));
  }

  //now get a reference to the copied array and return it
  svPtr_arrayRef = newRV_noinc(avPtr_filteredImage);
  return svPtr_arrayRef; // return the reference

}

END_OF_C_CODE

Reply via email to