Ken Williams wrote:


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.


I don't think that's true. I think you can call New() and Safefree() wherever you want, as long as the pointers you're using are valid. I don't think they know about scoping.


I think you are right. I ran the following and observed no memory leak.
If I run it without the release() call, I do see memory consumption increase - although it doesn't seem to increase at every iteration, which surprised me a little.


use warnings;

use Inline C => <<'EOC';

void create_destroy(int a) {
     char * temp;
     int i;

     Newz(123,temp, a, char);
     if(temp == NULL) croak("Failed to allocate memory");

for(i = 0; i < a; ++i) temp[i] = 'a';

release(temp);

}

void release(char * in) {
     Safefree(in);
}


EOC


print "Running ...";

for(1..100) {
   create_destroy(200000);
   sleep(1);
   }

__END__

------------------------------------------------------------------

Of course that avoids the problem of visiting "perl land" - simply by not going there.

As to how you would take a char string to "perl land" and back, and then free the memory is probably beyond me .... but I'll continue to think about it :-)

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