Skip to content

Commit 87b84fb

Browse files
mcfansmluisbrown
authored andcommitted
Improved way to get SwiftUI image handle (#945)
* Improved way to get SwiftUI image handle * Use #dsohandle instead of fatalerror * Search /SwiftUI instead and use dyld image header pointer instead of convert it Previous approch will crash on mac platform but ok with iOS simulator
1 parent cf4150e commit 87b84fb

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Sources/ComposableArchitecture/Internal/RuntimeWarnings.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if DEBUG
1+
#if DEBUG && canImport(os)
22
import os
33

44
// NB: Xcode runtime warnings offer a much better experience than traditional assertions and
@@ -8,9 +8,18 @@
88
// Feedback filed: https://gist.github.com/stephencelis/a8d06383ed6ccde3e5ef5d1b3ad52bbc
99
let rw = (
1010
dso: { () -> UnsafeMutableRawPointer in
11-
var info = Dl_info()
12-
dladdr(dlsym(dlopen(nil, RTLD_LAZY), "LocalizedString"), &info)
13-
return info.dli_fbase
11+
let count = _dyld_image_count()
12+
for i in 0..<count {
13+
if let name = _dyld_get_image_name(i) {
14+
let swiftString = String(cString: name)
15+
if swiftString.hasSuffix("/SwiftUI") {
16+
if let header = _dyld_get_image_header(i) {
17+
return UnsafeMutableRawPointer(mutating: UnsafeRawPointer(header))
18+
}
19+
}
20+
}
21+
}
22+
return UnsafeMutableRawPointer(mutating: #dsohandle)
1423
}(),
1524
log: OSLog(subsystem: "com.apple.runtime-issues", category: "ComposableArchitecture")
1625
)

0 commit comments

Comments
 (0)