diff options
| author | Bug Magnet <marco.nikic@mullvad.net> | 2025-05-23 13:35:32 +0200 |
|---|---|---|
| committer | Bug Magnet <marco.nikic@mullvad.net> | 2025-05-23 13:35:32 +0200 |
| commit | 976b143a220b1b65a2a4d7004cd40688c25da79b (patch) | |
| tree | f6cc7c07f02eb52a756e167f721e4322b96611f8 /ios | |
| parent | 3d0954f14315e3d58be5e2bb7f4c8b37bc18ac5f (diff) | |
| parent | 33b9ccf8c8da011cc5fb4cf065f1c7234f0e7174 (diff) | |
| download | mullvadvpn-976b143a220b1b65a2a4d7004cd40688c25da79b.tar.xz mullvadvpn-976b143a220b1b65a2a4d7004cd40688c25da79b.zip | |
Merge branch 'remove-dispatch-queue-hack'
Diffstat (limited to 'ios')
| -rw-r--r-- | ios/MullvadRustRuntime/MullvadAddressCacheProvider.swift | 16 | ||||
| -rw-r--r-- | ios/MullvadRustRuntime/include/mullvad_rust_runtime.h | 11 |
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 |
