PerlFFI / FFI-Platypus

Write Perl bindings to non-Perl libraries with FFI. No XS required.
89 stars 23 forks source link

Doc: add hint about using deferred execution to call free() to FFI::Platypus::Memory #378

Closed zmughal closed 1 year ago

zmughal commented 1 year ago
use Feature::Compat::Defer;
use FFI::Platypus::Memory qw(malloc free);

sub run {
  my $ptr = malloc(64);
  defer { free($ptr); }

  my $data = do_something($ptr);

  # do not need to place `free($ptr)` here as it will run through `defer`

  return $data;
}

See:

plicease commented 1 year ago

Thanks for writing this down, I put some copy into #384 relating to this.