From 297a71011e28a25a12176261e23bb00b3df92165 Mon Sep 17 00:00:00 2001 From: Manuel Burghard Date: Sun, 15 Mar 2020 15:48:20 +0100 Subject: [PATCH] Fix leak due to missing free after a host function call. --- Package.swift | 4 +++- Runtime/src/index.ts | 2 ++ Sources/JavaScriptKit/JSFunction.swift | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 626a5e780..47205c2a1 100644 --- a/Package.swift +++ b/Package.swift @@ -16,7 +16,9 @@ let package = Package( "-Xlinker", "--export=swjs_call_host_function", "-Xlinker", - "--export=swjs_prepare_host_function_call" + "--export=swjs_prepare_host_function_call", + "-Xlinker", + "--export=swjs_cleanup_host_function_call" ]) ]), .target( diff --git a/Runtime/src/index.ts b/Runtime/src/index.ts index 5f6b7d147..d7642f323 100644 --- a/Runtime/src/index.ts +++ b/Runtime/src/index.ts @@ -11,6 +11,7 @@ declare const global: GlobalVariable; interface SwiftRuntimeExportedFunctions { swjs_prepare_host_function_call(size: number): pointer; + swjs_cleanup_host_function_call(argv: pointer): void; swjs_call_host_function( host_func_id: number, argv: pointer, argc: number, @@ -112,6 +113,7 @@ export class SwiftRuntime { output = result }) exports.swjs_call_host_function(host_func_id, argv, argc, callback_func_ref) + exports.swjs_cleanup_host_function_call(argv) return output } diff --git a/Sources/JavaScriptKit/JSFunction.swift b/Sources/JavaScriptKit/JSFunction.swift index 26794241e..211988d90 100644 --- a/Sources/JavaScriptKit/JSFunction.swift +++ b/Sources/JavaScriptKit/JSFunction.swift @@ -76,6 +76,11 @@ public func _prepare_host_function_call(_ argc: Int32) -> UnsafeMutableRawPointe return malloc(Int(argumentSize))! } +@_cdecl("swjs_cleanup_host_function_call") +public func _cleanup_host_function_call(_ pointer: UnsafeMutableRawPointer) { + free(pointer) +} + @_cdecl("swjs_call_host_function") public func _call_host_function( _ hostFuncRef: JavaScriptHostFuncRef,