typedef struct MyClient MyClient;
// to init
void init_my_client(struct MyClient** client_ptr) {
MyClient* client = init_impl()
// when calling in swift, this doesn't work
*client_ptr = client
}
// to destroy
void destroy(struct MyClient* client_ptr) {
free(client_ptr)
}
When I imported this header file into swift, it infers type struct MyClient** into UnsafeMutablePointer<OpaquePointer?>?
So I tried the following way to init it, when I assign value to this pointer in C, it reports Exit code 11...
MyClient.swift
public class MyClient {
let clientPtr: UnsafeMutablePointer<OpaquePointer?>?
init() {
init_my_client(clientPtr)
// the unexpected exit happens inside this C function, during assigning my client pointer to clientPtr
}
deinit() {
if let ptr = clientPtr {
if let innerPtr = ptr.pointee {
destroy(innerPtr)
clientPtr = nil
}
}
}
}
I am very new to swift, could anyone here help to point out what's the problem exactly?
I have a felling on that, before passing clientPtr into init_my_client, it's not been initialized (with value nil?), but I am not aware of the right way to do so.
This seems to work, but the reason why I'd passing in the pointer object is that, in the real implementation, there is a return code on both newClient and deleteClient indicating if the operation was done correctly.