in reply to Memory Management...
notundef $x;
The former is equivalent to the C "free" function, whereas with the latter you are moving your pointer/reference off your object - if this were C code, you'd have just lost your allocated memory and leaked it. This is Perl, so the garbage collector takes care of you and it works, but not for the reason you think it does.$x = undef;
When I did this with SWIG/C/Perl/Tcl, I vaguely remember having to undef the $pointer that held the struct or I would get a fatal exception from the garbage collector. However, that may have been in Tcl, not Perl. And I may have been doing something wrong. I'll update this when I get home and can look at the code...use SwiggedCLib; #allocate a struct in C my $pointer = SwiggedCLib::createIt(); #do something with the struct my $result = SwiggedCLib::doSomething( $pointer ); #destroy the struct - Perl doesn't know how SwiggedCLib::destroyIt( $pointer );
after freeing the memory using a SWIGged C function (which also set the C pointer to NULL). The error I got if I didn't may have been a segfault - I don't remember, I just know it was a showstopper. It leaves open the possibility that I was doing something wrong - it worked fine in Tcl, though...undef $pointer;
|
|---|