PerlFFI / FFI-Platypus

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

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

Closed zmughal closed 2 years ago

zmughal commented 2 years 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 2 years ago

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