David Christopher Asher wrote:


HOWEVER, if I split it into two functions:


char * test_image_part_1(int size) {
    char * image;
    New(123,image,size,char);
    memset(image,CLEAR,size);
    return image;
 }

void test_image_part_2(char * image) {
    Safefree( (void *) image);
 }


As I understand it, memory that has been allocated in a subroutine must be freed in that *same* subroutine. And you can free only that memory which has been dynamically allocated in the subroutine. In the second sub (above), 'image' has been passed as an arg, the memory has not been dynamically allocated by that subroutine, and I am not surprised that an attempt to Safefree() it results in a segfault.


What you are probably after is:

 SV * test_image_part_1(int size) {
     char * image;
     SV * out;
     New(123,image,size,char);
     memset(image,CLEAR,size);
     out = newSVpv(out, size);
// or 'out = newSVpv(out, 0);' and the size is determined for you
     Safefree(image);
     return out;
  }

Now the dynamically allocated memory has been freed and perl will take care of 'out' for you, so there's no need for a clean-up sub.

So, having written some perl code:
my $var = test_image_part_1($size);
the question of how to free the memory allocated to $var is a purely perl question. It gets freed for you when the application terminates, but if you want to free it before that, then I think there's something in the perlfaq about that.


Or, have I misunderstood your aims ? - are you trying to use Inline::CPP to free-up the memory being used to store a perl variable (prior to the termination of the program) ? I think that would be difficult.

Hth.

Cheers,
Rob



--
Any emails containing attachments will be deleted from my ISP's mail server before I even get to see them. If you wish to email me an attachment, please provide advance warning so that I can make the necessary arrangements.




Reply via email to