in reply to FFI::Platypus: Replace malloc with GC_MALLOC?
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.)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: FFI::Platypus: Replace malloc with GC_MALLOC?
by syphilis (Archbishop) on Dec 14, 2022 at 22:49 UTC | |
by ikegami (Patriarch) on Dec 15, 2022 at 20:48 UTC | |
by syphilis (Archbishop) on Dec 16, 2022 at 02:02 UTC | |
Re^2: FFI::Platypus: Replace malloc with GC_MALLOC?
by karlgoethebier (Abbot) on Dec 13, 2022 at 16:24 UTC |