Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory leaks in WasmInterpreter.swift #18

Open
gossmer opened this issue Sep 20, 2022 · 0 comments
Open

memory leaks in WasmInterpreter.swift #18

gossmer opened this issue Sep 20, 2022 · 0 comments

Comments

@gossmer
Copy link

gossmer commented Sep 20, 2022

I think there are leaks in these 2 methods. The 2 places "UnsafeMutablePointer*.allocate" is called there is no dealloc (or a defer dealloc). Under instruments in Xcode these show as leaks of 16 bytes for each call made to these methods.

func _call(_ function: IM3Function, args: [String]) throws {
    try args.withCStrings { cStrings throws -> Void in
        var mutableCStrings = cStrings
        let size = UnsafeMutablePointer<Int>.allocate(capacity: 1)   <-- here
        let r = wasm3_CallWithArgs(
            function,
            UInt32(args.count),
            &mutableCStrings,
            size,
            nil
        )
        if let result = r {
            throw WasmInterpreterError.onCallFunction(String(cString: result))
        } else if size.pointee != 0 {
            throw WasmInterpreterError.invalidFunctionReturnType
        } else {
            return ()
        }
    }
}

func _call<T: WasmTypeProtocol>(_ function: IM3Function, args: [String]) throws -> T {
    try args.withCStrings { cStrings throws -> T in
        var mutableCStrings = cStrings
        let size = UnsafeMutablePointer<Int>.allocate(capacity: 1)    <-- here
        let output = UnsafeMutablePointer<T>.allocate(capacity: 1)   <-- not sure since we return pointee, but may be ok.
        let r = wasm3_CallWithArgs(
            function,
            UInt32(args.count),
            &mutableCStrings,
            size,
            output
        )
        if let result = r {
            throw WasmInterpreterError.onCallFunction(String(cString: result))
        } else if MemoryLayout<T>.size != size.pointee {
            throw WasmInterpreterError.invalidFunctionReturnType
        } else {
            return output.pointee
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant