On Friday, November 14, 2003, at 04:19 PM, Sisyphus wrote:
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.
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 the problem here is that Inline is using the "char *" type as a synonym for "string", and the addresses may not be the same in your two functions because the address may have changed when you use the string in Perl land. You should probably store a pointer to the actual address you're using. See the Inline-C Cookbook.
However, I haven't tested this theory. Someone with a concrete example would probably be appreciated.
-Ken