#! /usr/bin/env perl6 use v6; use NativeCall; class Foo { has OpaquePointer $!repr; sub make_foo() returns OpaquePointer is native('libstandalone.dylib') {} sub destroy_foo(OpaquePointer) is native('libstandalone.dylib') {} sub foo_getx(OpaquePointer) returns int is native('libstandalone.dylib') {} sub foo_setx(OpaquePointer, int) is native('libstandalone.dylib') {} submethod BUILD() { $!repr = make_foo(); } method end() { destroy_foo($!repr); } method get() { return foo_getx($!repr); } method set(Int $val) { foo_setx($!repr, $val); } method x() is rw { my $foo := $!repr; return Proxy.new: FETCH => method () { # Change this to $foo instead of $!repr, and it works return foo_getx($!repr); }, STORE => method ($val) { foo_setx($foo, $val); }; } } sub MAIN() { my $foo = Foo.new; say "1: ", $foo.x; $foo.x = 42; say "2: ", $foo.x; $foo.end; say "Done."; } # vim:set ft=perl6: #include #include /* Stand-alone test case for segfault w/ NativeCall (tested w/ Rakudo* 2012.06) On OS X, compile and link with: gcc -c standalone.c && libtool -dynamic -o libstandalone.dylib standalone.o */ struct Foo { int x; }; int foo_getx(struct Foo *foo) { return foo->x; } void foo_setx(struct Foo *foo, int x) { foo->x = x; } struct Foo *make_foo(void) { struct Foo *foo = (struct Foo *)malloc(sizeof(struct Foo)); foo->x = 0; } void destroy_foo(struct Foo *foo) { assert(foo != NULL && "destroy_foo(NULL)"); free(foo); }