summaryrefslogtreecommitdiffhomepage
path: root/ios
diff options
context:
space:
mode:
Diffstat (limited to 'ios')
-rw-r--r--ios/MullvadRustRuntime/MullvadAddressCacheProvider.swift16
-rw-r--r--ios/MullvadRustRuntime/include/mullvad_rust_runtime.h11
2 files changed, 16 insertions, 11 deletions
diff --git a/ios/MullvadRustRuntime/MullvadAddressCacheProvider.swift b/ios/MullvadRustRuntime/MullvadAddressCacheProvider.swift
index 4c731eaf3a..5de81c733e 100644
--- a/ios/MullvadRustRuntime/MullvadAddressCacheProvider.swift
+++ b/ios/MullvadRustRuntime/MullvadAddressCacheProvider.swift
@@ -15,17 +15,13 @@ public func iniSwiftAddressCacheWrapper(provider: DefaultAddressCacheProvider) -
}
@_cdecl("swift_get_cached_endpoint")
-func getCacheEndpoint(rawAddressCacheProvider: UnsafeMutableRawPointer) -> UnsafePointer<CChar>! {
+func getCacheEndpoint(rawAddressCacheProvider: UnsafeMutableRawPointer) -> LateStringDeallocator {
let addressCacheProvider = Unmanaged<DefaultAddressCacheProvider>.fromOpaque(rawAddressCacheProvider)
.takeUnretainedValue()
let cStr = addressCacheProvider.getCurrentEndpoint().description.toCStringPointer()
- /**
- `cStr` needs to shortly outlive the return of this function in order to get transformed into a `SocketAddr`
- This is the simplest way to guarantee that the pointer returned does not get deallocated immediately
- Or that no memory is leaked every time this function gets called
- **/
- DispatchQueue(label: "com.MullvadRustRuntime.DecallocateQueue").async {
- cStr?.deallocate()
- }
- return cStr
+ return LateStringDeallocator(ptr: cStr, deallocate_ptr: deallocate_pointer(pointer:))
+}
+
+func deallocate_pointer(pointer: UnsafePointer<CChar>?) {
+ pointer?.deallocate()
}
diff --git a/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h b/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
index 70c1428098..08bb177116 100644
--- a/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
+++ b/ios/MullvadRustRuntime/include/mullvad_rust_runtime.h
@@ -77,6 +77,15 @@ typedef struct SwiftRetryStrategy {
struct RetryStrategy *_0;
} SwiftRetryStrategy;
+/**
+ * A struct used to deallocate a pointer to a C String later than when the pointer's control is relinquished from Swift.
+ * Use the `deallocate_ptr` function on `ptr` to call the custom deallocator provided by Swift.
+ */
+typedef struct LateStringDeallocator {
+ const char *ptr;
+ void (*deallocate_ptr)(const char*);
+} LateStringDeallocator;
+
typedef struct SwiftMullvadApiResponse {
uint8_t *body;
uintptr_t body_size;
@@ -316,7 +325,7 @@ struct SwiftCancelHandle mullvad_ios_delete_account(struct SwiftApiContext api_c
* `rawAddressCacheProvider` **must** be provided by a call to `init_swift_address_cache_wrapper`
* It is okay to persist it, and use it accross multiple threads.
*/
-extern const char *swift_get_cached_endpoint(const void *rawAddressCacheProvider);
+extern struct LateStringDeallocator swift_get_cached_endpoint(const void *rawAddressCacheProvider);
/**
* Called by the Swift side in order to provide an object to rust that provides API addresses in a UTF-8 string form