            MemorySegment resultPtr = (MemorySegment) {{ ffi_handle }}.invoke({{ args_joined }});
{{ named_frees }}            if (resultPtr.equals(MemorySegment.NULL)) {
                checkLastFfiError();
                return null;
            }
            try {
                MemorySegment jsonPtr = (MemorySegment) {{ to_json }}.invoke(resultPtr);
                if (jsonPtr.equals(MemorySegment.NULL)) {
                    checkLastFfiError();
                    throw new {{ exception_class }}("{{ method_name }}: failed to serialize response", (Throwable) null);
                }
                String json = jsonPtr.reinterpret(Long.MAX_VALUE).getString(0);
                NativeLib.{{ prefix_upper }}_FREE_STRING.invoke(jsonPtr);
                return STREAM_MAPPER.readValue(json, {{ return_type_name }}.class);
            } finally {
                {{ ret_free }}.invoke(resultPtr);
            }
