in reply to Attempt to copy freed scalar and return value semantics

Probably the context.

Maybe you could try:

return scalar $code->($self, @_);

and see you you still get the panic.