The issue is that you don't want to forget to free the buffer.
Another approach is to not use FFI directly, but hide the glue in a module, where you could use an END block to ensure it's freed on exit.
package My::StringUtils; use strict; use warnings; use Export qw( import ); use FFI::Platypus 2.00; our @EXPORT_OK = qw( string_reverse ); my $ffi = FFI::Platypus->new( api => 2, lib => './string_reverse.so', ); $ffi->attach( string_reverse => [ 'string' ] => 'string' ); END { string_reverse( undef ); } 1
use strict; use warnings; use feature qw( say ); use My::StringUtils qw( string_reverse ); say string_reverse( "Hello world" );
(Fine, you don't need a module to use END, but using a module means you don't forget to use END.)
In reply to Re: FFI::Platypus: Replace malloc with GC_MALLOC?
by ikegami
in thread FFI::Platypus: Replace malloc with GC_MALLOC?
by karlgoethebier
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |