Using Xcode 13's new binding variant of ForEach my app crashes predictably on-device (but works in the simulator) when deleting any element other than the last one from content backed by an #Published property.
Strangely, this only happens when the view is contained within a NavigationView and has a sheet attached.
Here is a self-contained app demonstrating the issue:
import SwiftUI
struct Vegetable: Identifiable {
let id: UUID
let name: String
}
class MyModel: ObservableObject {
#Published var vegetables: [Vegetable] = [
Vegetable(id: UUID(), name: "Eggplant"),
Vegetable(id: UUID(), name: "Corn"),
Vegetable(id: UUID(), name: "Radish")
]
}
#main
struct SwiftUIForEachTestApp: App {
#StateObject var model = MyModel()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(model)
}
}
}
struct ContentView: View {
var body: some View {
// Remove the containing NavigationView and the crash disappears
NavigationView {
MyView()
}
}
}
struct MyView: View {
#EnvironmentObject private var model: MyModel
#State private var isPresentingSheet = false
var body: some View {
List {
ForEach($model.vegetables) { $vegetable in
Text(vegetable.name)
}
.onDelete { indices in model.vegetables.remove(atOffsets: indices) }
}
// Remove the sheet and the crash disappears
.sheet(isPresented: $isPresentingSheet) {
}
}
}
Running the app and deleting the top row results in the following crash (line 16 corresponds to #main):
#0 0x00000001a73a6eac in _swift_runtime_on_report ()
#1 0x00000001a741f100 in _swift_stdlib_reportFatalErrorInFile ()
#2 0x00000001a705e668 in closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) ()
#3 0x00000001a705dce0 in _assertionFailure(_:_:file:line:flags:) ()
#4 0x00000001a703c6b4 in _ArrayBuffer._checkInoutAndNativeTypeCheckedBounds(_:wasNativeTypeChecked:) ()
#5 0x00000001a7041e2c in Array.subscript.read ()
#6 0x00000001a7041d58 in protocol witness for Collection.subscript.read in conformance [τ_0_0] ()
#7 0x0000000100320c38 in closure #1 in ForEach<>.init<τ_0_0>(_:id:content:) ()
#8 0x0000000100320f48 in partial apply for closure #1 in ForEach<>.init<τ_0_0>(_:id:content:) ()
#9 0x0000000100320fd8 in thunk for #escaping #callee_guaranteed (#in_guaranteed τ_1_0.Collection.Index) -> (#out τ_1_0.Collection.Index, #out τ_0_1) ()
#10 0x0000000100321090 in partial apply for thunk for #escaping #callee_guaranteed (#in_guaranteed τ_1_0.Collection.Index) -> (#out τ_1_0.Collection.Index, #out τ_0_1) ()
#11 0x00000001a7193d0c in LazyMapSequence<>.subscript.read ()
#12 0x00000001a71938e4 in protocol witness for Collection.subscript.read in conformance <> LazyMapSequence<τ_0_0, τ_0_1> ()
#13 0x00000001aa2a374c in ForEach.IDGenerator.makeID(data:index:offset:) ()
#14 0x00000001aa2abf54 in ForEachState.ForEachViewIDCollection.subscript.getter ()
#15 0x00000001aa2ac914 in ForEachState.ForEachViewIDCollection.subscript.read ()
#16 0x00000001aa2ac84c in protocol witness for Collection.subscript.read in conformance ForEachState<τ_0_0, τ_0_1, τ_0_2>.ForEachViewIDCollection ()
#17 0x00000001aa37a838 in _ViewList_ID._Views.subscript.getter ()
#18 0x00000001aa37a3f4 in protocol witness for Collection.subscript.read in conformance _ViewList_ID.Views ()
#19 0x00000001a71eaecc in Slice.subscript.getter ()
#20 0x00000001a71ed008 in Slice.subscript.read ()
#21 0x00000001a71ecf48 in protocol witness for Collection.subscript.read in conformance Slice<τ_0_0> ()
#22 0x00000001a7193cc8 in LazyMapSequence<>.subscript.read ()
#23 0x00000001a71938e4 in protocol witness for Collection.subscript.read in conformance <> LazyMapSequence<τ_0_0, τ_0_1> ()
#24 0x00000001aa157f44 in ShadowRowCollection.subscript.getter ()
#25 0x00000001aa1580ec in ShadowRowCollection.subscript.read ()
#26 0x00000001aa15802c in protocol witness for Collection.subscript.read in conformance ShadowRowCollection<τ_0_0> ()
#27 0x00000001aa365448 in ListCoreDataSource.rowID(at:) ()
#28 0x00000001aa004ec0 in closure #1 in performUpdates #1 <τ_0_0, τ_0_1>() in ListCoreCoordinator.updateUITableView(_:to:transaction:) ()
#29 0x00000001a9ffd09c in specialized _ArrayProtocol.filter(_:) ()
#30 0x00000001aa004658 in performUpdates #1 <τ_0_0, τ_0_1>() in ListCoreCoordinator.updateUITableView(_:to:transaction:) ()
#31 0x00000001aa00565c in closure #1 in ListCoreCoordinator.updateUITableView(_:to:transaction:) ()
#32 0x00000001a9ec6008 in thunk for #escaping #callee_guaranteed () -> () ()
#33 0x00000001a9eaee84 in static Update.end() ()
#34 0x00000001a9cd7390 in static NSRunLoop.flushObservers() ()
#35 0x00000001a9cd72d8 in closure #1 in closure #1 in static NSRunLoop.addObserver(_:) ()
#36 0x00000001a9cd2250 in specialized thunk for #callee_guaranteed () -> (#error #owned Error) ()
#37 0x00000001c9b53f24 in autoreleasepool<τ_0_0>(invoking:) ()
#38 0x00000001a9cd72b8 in closure #1 in static NSRunLoop.addObserver(_:) ()
#39 0x00000001a9cd7410 in #objc closure #1 in static NSRunLoop.addObserver(_:) ()
#40 0x00000001a3437588 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#41 0x00000001a34316ac in __CFRunLoopDoObservers ()
#42 0x00000001a3431c58 in __CFRunLoopRun ()
#43 0x00000001a3431308 in CFRunLoopRunSpecific ()
#44 0x00000001baab4734 in GSEventRunModal ()
#45 0x00000001a5eaf75c in -[UIApplication _run] ()
#46 0x00000001a5eb4fcc in UIApplicationMain ()
#47 0x00000001aa39c380 in closure #1 in KitRendererCommon(_:) ()
#48 0x00000001aa39c30c in runApp<τ_0_0>(_:) ()
#49 0x00000001a9e94b38 in static App.main() ()
#50 0x000000010031c9c4 in static SwiftUIForEachTestApp.$main() at /xxx/SwiftUIForEachTest/Shared/ContentView.swift:16
#51 0x000000010031e888 in main ()
#52 0x00000001a30edcf8 in start ()
Is this a bug in SwiftUI?
works very well for me on macos 12.beta, xcode 13.beta, target ios 15 and macCatalyst.
Tested on mac, iPhone and iPad. Maybe it is different on older systems.
EDIT: using EnvironmentObject, this is the code I'm using in my tests:
import SwiftUI
#main
struct TestApp: App {
#StateObject var model = MyModel() // <---
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(model)
}
}
}
struct ContentView: View {
#EnvironmentObject var model: MyModel // <---
var body: some View {
NavigationView {
MyView()
}
}
}
struct MyView: View {
#EnvironmentObject var model: MyModel // <---
#State private var isPresentingSheet = false
var body: some View {
List {
ForEach($model.vegetables) { $vegetable in
Text(vegetable.name)
}
.onDelete { indices in model.vegetables.remove(atOffsets: indices) }
}
.sheet(isPresented: $isPresentingSheet) {
Text("the sheet") // <----
}
}
}
struct Vegetable: Identifiable {
let id: UUID
let name: String
}
class MyModel: ObservableObject {
#Published var vegetables: [Vegetable] = [
Vegetable(id: UUID(), name: "Eggplant"),
Vegetable(id: UUID(), name: "Corn"),
Vegetable(id: UUID(), name: "Radish")
]
}
Related
I'm running an optimization loop that repeatedly calls ComputeSignedDistancePairwiseClosestPoints. After hundreds of cumulative hours of running this without problems, I've encountered a segfault and am looking for guidance on debugging it (or if it's a bug in Drake, possible temporary workarounds).
As suggested in this post, upon initialization, I store diagram, plant, and scene graph contexts, and before calling ComputeSignedDistancePairwiseClosestPoints, I call the query object output port evaluated at the stored scene graph context. Here is a rough outline of the relevant code structure:
class SimplifiedClass:
def __init__(self, ...):
# initializing plant, contexts, and query object port
self.diagram = function_generating_diagram_with_plant(...)
self.plant = self.diagram.GetSubsystemByName("plant")
self.scene_graph = self.diagram.GetSubsystemByName("scene_graph")
_diag_context = self.diagram.CreateDefaultContext()
self.plant_context = self.plant.GetMyMutableContextFromRoot(_diag_context)
self.sg_context = self.scene_graph.GetMyMutableContextFromRoot(_diag_context)
self.qo_port = self.scene_graph.get_query_output_port()
#property
def query_object(self):
return self.qo_port.Eval(self.sg_context)
def function_that_computes_signed_distances(self, ...):
# this function calls ComputeSignedDistancePairClosestPoints()
# it is called repeatedly in an optimization loop
# line that computes signed distance pairs after other operations
sdps = self.query_object.ComputeSignedDistancePairwiseClosestPoints(
max_distance=some_number
)
I can reproduce the segfault by fixing the random seed appropriately, and I find that when changing the max_distance parameter of ComputeSignedDistancePairwiseClosestPoints, the segfault no longer occurs for this specific seed.
I printed a gdb backtrace which returns the following:
#0 0x00007f21a9149be5 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#1 0x00007f21a914cc46 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#2 0x00007f21a914cca5 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#3 0x00007f21a914cca5 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#4 0x00007f21a914cca5 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#5 0x00007f21a914cca5 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#6 0x00007f21a914cca5 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#7 0x00007f21a914cca5 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#8 0x00007f21a914cca5 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#9 0x00007f21a914d61b in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#10 0x00007f21a914ba3a in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#11 0x00007f21a914bbdc in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#12 0x00007f21a919f324 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#13 0x00007f21a9165123 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#14 0x00007f21a918961a in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#15 0x00007f21a918d29d in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#16 0x00007f21a918fdd2 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#17 0x00007f21a92a9496 in void drake::geometry::internal::shape_distance::CalcDistanceFallback<double>(fcl::CollisionObject<double> const&, fcl::CollisionObject<double> const&, fcl::DistanceRequest<double> const&, drake::geometry::SignedDistancePair<double>*) () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#18 0x00007f21a92a9aa5 in bool drake::geometry::internal::shape_distance::Callback<double>(fcl::CollisionObject<double>*, fcl::CollisionObject<double>*, void*, double&) ()
from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#19 0x00007f21a91e98ca in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#20 0x00007f21a91e99b2 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#21 0x00007f21a91e998b in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#22 0x00007f21a91e998b in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#23 0x00007f21a91e998b in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#24 0x00007f21a91e99b2 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#25 0x00007f21a91e99b2 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#26 0x00007f21a91e998b in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#27 0x00007f21a91e9a26 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#28 0x00007f21a933a01e in drake::geometry::internal::ProximityEngine<double>::ComputeSignedDistancePairwiseClosestPoints(std::unordered_map<drake::geometry::GeometryId, drake::math::RigidTransform<double>, std::hash<drake::geometry::GeometryId>, std::equal_to<drake::geometry::GeometryId>, std::allocator<std::pair<drake::geometry::GeometryId const, drake::math::RigidTransform<double> > > > const&, double) const ()
from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#29 0x00007f21a93ab1fc in drake::geometry::QueryObject<double>::ComputeSignedDistancePairwiseClosestPoints(double) const () from /opt/drake/lib/python3.10/site-packages/pydrake/common/../../../../libdrake.so
#30 0x00007f20bf6f2327 in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/geometry.cpython-310-x86_64-linux-gnu.so
#31 0x00007f20bf547c4f in ?? () from /opt/drake/lib/python3.10/site-packages/pydrake/geometry.cpython-310-x86_64-linux-gnu.so
#32 0x00005604e39c1b5e in ?? ()
#33 0x00005604e39b87db in _PyObject_MakeTpCall ()
#34 0x00005604e39d066b in ?? ()
#35 0x00005604e39ac2fc in _PyEval_EvalFrameDefault ()
#36 0x00005604e39c23ac in _PyFunction_Vectorcall ()
#37 0x00005604e39ab14a in _PyEval_EvalFrameDefault ()
#38 0x00005604e39d0391 in ?? ()
#39 0x00005604e39ac2fc in _PyEval_EvalFrameDefault ()
#40 0x00005604e39c23ac in _PyFunction_Vectorcall ()
#41 0x00005604e39ab14a in _PyEval_EvalFrameDefault ()
#42 0x00005604e39c23ac in _PyFunction_Vectorcall ()
... (more in this trace omitted since it's not Drake-specific)
Below is my sample code. Everything working perfectly but if trying to add a new property in PageLoad or TestDetailResponseModel struct then the app is getting crash. There is no issue in API response and parsing. Getting parsed value in the print statement. According to the crash log, it's something related to memory but I am unable to figure it out. Below is my sample code. My API response is approx 40 KB. Please help me.
Note-
I am not mutating the response objects only consuming.
If change PageLoad from struct to class app is not crashing
import RxCocoa
import RxSwift
public struct TestDetailViewModel {
private func getTestDetailSuccessResponse() -> Driver<TestDetailResponseModel?> {
//Make API call asynchronously using RxSwift and storing result in testDetailResponseModel
let testDetailResponseModel: TestDetailResponseModel? = RXNetworkManager.execute(request: MyRequestModel) {
//Api Response
//Parse response. Parsing is proper. responseModel prints all the parsed properties with correct values of TestDetailResponseModel and PageLoad objects (detail, and pageNumber)
print(responseModel)
}
return Driver.just(testDetailResponseModel)
}
private func loadDetail() {
let isDetailExist = getTestDetailSuccessResponse().compactMap { response -> Bool? in
print(response) //No Crash
print(response?.pageLoad) //App crashed here if add pageNumber property in PageLoad object
return response?.pageLoad != nil
}
}
}
public struct TestDetailResponseModel: Decodable {
public let pageLoad: PageLoad?
//...
}
public struct PageLoad: Decodable {
public let detail: String?
//...
public let pageNumber: Double? //New property
}
=================Crash Report for same====================
Exception Type: EXC_BAD_ACCESS (SIGABRT)
Exception Subtype: KERN_INVALID_ADDRESS at 0x50a01fb550981f18 -> 0xffff9fb550981f18 (possible pointer authentication failure)
Exception Codes: 0x0000000000000001, 0x50a01fb550981f18
VM Region Info: 0xffff9fb550981f18 is not in any region. Bytes after previous region: 18446532646169747225
REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL
MALLOC_NANO (reserved) 600018000000-600020000000 [128.0M] rw-/rwx SM=NUL ...(unallocated)
--->
UNUSED SPACE AT END
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 0
Application Specific Information:
=================================================================
==17015==ERROR: AddressSanitizer: SEGV on unknown address 0x50a01fb550981f18 (pc 0x00018f9e1d50 bp 0x00016bbf60c0 sp 0x00016bbf6080 T0)
==17015==The signal is caused by a UNKNOWN memory access.
#0 0x18f9e1d50 in swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int)+0x2c (libswiftCore.dylib:arm64+0x302d50)
#1 0x18f9b20bc in _swift_retain_+0x54 (libswiftCore.dylib:arm64+0x2d30bc)
#2 0x10d8d5edc (liboainject.dylib:arm64+0x5edc)
#3 0x18f9e7958 in swift_bridgeObjectRetain+0x30 (libswiftCore.dylib:arm64+0x308958)
#4 0x1191ab100 in outlined copy of PageLoad?+0x1dc (TestCore:arm64+0x10ff100)
#5 0x1196cf554 in outlined copy of TestDetailResponseModel?+0x6a4 (TestCore:arm64+0x1623554)
#6 0x11a1541bc in TestDetailViewModel.getTestDetailSuccessResponse(response:) TestDetailViewModel+APICalls.swift:69
#7 0x1195240b8 in closure #13 in TestDetailViewModel.transform(input:) TestDetailViewModel.swift:108
#8 0x10e59b30c in CompactMapSink.on(_:) CompactMap.swift:47
#9 0x10e59b550 in protocol witness for ObserverType.on(_:) in conformance CompactMapSink<A, B> <compiler-generated>
#10 0x10e5f91d0 in partial apply for implicit closure #2 in implicit closure #1 in ShareWhileConnectedConnection._synchronized_subscribe<A>(_:) <compiler-generated>
#11 0x10e58d8d8 in thunk for #escaping #callee_guaranteed (#in_guaranteed Event<A>) -> () <compiler-generated>
#12 0x10e5f804c in ShareReplay1WhileConnectedConnection.on(_:)+0x70 (RxSwift:arm64+0x8804c)
#13 0x10e5f7bf4 in protocol witness for ObserverType.on(_:) in conformance ShareReplay1WhileConnectedConnection<A> <compiler-generated>
#14 0x10e5b0514 in Sink.forwardOn(_:) Sink.swift:34
#15 0x10e582398 in SwitchSinkIter._synchronized_on(_:) Switch.swift:160
#16 0x10e5abb68 in SynchronizedOnType.synchronizedOn(_:) SynchronizedOnType.swift:16
#17 0x10e582488 in protocol witness for ObserverType.on(_:) in conformance SwitchSinkIter<A, B, C> <compiler-generated>
#18 0x10e5f91d0 in partial apply for implicit closure #2 in implicit closure #1 in ShareWhileConnectedConnection._synchronized_subscribe<A>(_:) <compiler-generated>
#19 0x10e58d8d8 in thunk for #escaping #callee_guaranteed (#in_guaranteed Event<A>) -> () <compiler-generated>
#20 0x10e5f804c in ShareReplay1WhileConnectedConnection.on(_:)+0x70 (RxSwift:arm64+0x8804c)
#21 0x10e5f7bf4 in protocol witness for ObserverType.on(_:) in conformance ShareReplay1WhileConnectedConnection<A> <compiler-generated>
#22 0x10e5b0514 in Sink.forwardOn(_:) Sink.swift:34
#23 0x10e59d0c8 in CatchSink.on(_:) Catch.swift:131
#24 0x10e59d31c in protocol witness for ObserverType.on(_:) in conformance CatchSink<A> <compiler-generated>
#25 0x10e605534 in closure #1 in ObserveOnSerialDispatchQueueSink.init(scheduler:observer:cancel:) ObserveOn.swift:184
#26 0x10e605d48 in partial apply for thunk for #escaping #callee_guaranteed (#guaranteed ObserveOnSerialDispatchQueueSink<A>, #in_guaranteed Event<A.ObserverType.Element>) -> (#out Disposable) <compiler-generated>
#27 0x10e5904e4 in partial apply for closure #1 in MainScheduler.scheduleInternal<A>(_:action:) <compiler-generated>
#28 0x10e585410 in thunk for #escaping #callee_guaranteed () -> () <compiler-generated>
#29 0x1139f950c in __wrap_dispatch_async_block_invoke+0xb8 (libclang_rt.asan_iossim_dynamic.dylib:arm64+0x3d50c)
#30 0x18010d240 in _dispatch_call_block_and_release+0x14 (libdispatch.dylib:arm64+0x2240)
#31 0x18010ea94 in _dispatch_client_callout+0xc (libdispatch.dylib:arm64+0x3a94)
#32 0x18011c418 in _dispatch_main_queue_drain+0x3cc (libdispatch.dylib:arm64+0x11418)
#33 0x18011c038 in _dispatch_main_queue_callback_4CF+0x24 (libdispatch.dylib:arm64+0x11038)
#34 0x180361c28 in __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__+0x8 (CoreFoundation:arm64+0x85c28)
#35 0x18035c0ac in __CFRunLoopRun+0x97c (CoreFoundation:arm64+0x800ac)
#36 0x18035b214 in CFRunLoopRunSpecific+0x238 (CoreFoundation:arm64+0x7f214)
#37 0x18c25f608 in GSEventRunModal+0x9c (GraphicsServices:arm64+0x3608)
#38 0x184d88a94 in -[UIApplication _run]+0x3dc (UIKitCore:arm64+0xc0da94)
#39 0x184d8d630 in UIApplicationMain+0x6c (UIKitCore:arm64+0xc12630)
#40 0x104dc5958 in main AppDelegate.swift:32
#41 0x10b66dcd4 in start_sim+0x10 (dyld_sim:arm64+0x1cd4)
#42 0x10b71d084 (<unknown module>)
#43 0xa777ffffffffffc (<unknown module>)
==17015==Register values:
x[0] = 0x000000018035ba08 x[1] = 0xaa1403f6aa1303e1 x[2] = 0x0000000000000001 x[3] = 0x0000000000000001
x[4] = 0x0000000000000000 x[5] = 0x0000000000000000 x[6] = 0x0000000000000000 x[7] = 0x0000000000000000
x[8] = 0x50a01fb550981f08 x[9] = 0x000000018035ba00 x[10] = 0xffffffffffffffff x[11] = 0xaa1403f8aa1303e1
x[12] = 0x0000000000000000 x[13] = 0x0000000000000000 x[14] = 0x0000000000000000 x[15] = 0x0000000000000000
x[16] = 0x00000001cc0ac98c x[17] = 0x0000000000000000 x[18] = 0x0000000000000000 x[19] = 0x000000016bbf6118
x[20] = 0x000000018035ba00 x[21] = 0x000000010d8dc000 x[22] = 0x000000016bbf60f8 x[23] = 0x000000010d8dc000
x[24] = 0x000000010d8dc000 x[25] = 0x0000000000000000 x[26] = 0x40c1d7097c1bda51 x[27] = 0x4000000153eb88d0
x[28] = 0xc000000000000030 fp = 0x000000016bbf60c0 lr = 0x000000018f9b20c0 sp = 0x000000016bbf6080
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (libswiftCore.dylib:arm64+0x302d50) in swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int)+0x2c
==17015==ABORTING
dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode_13.4.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_INSERT_LIBRARIES=/Applications/Xcode_13.4.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/DVTInstrumentsFoundation.framework/liboainject.dylib
CoreSimulator 802.6.1 - Device: iPhone 13 Pro Max (********-****-****-****-************) - Runtime: iOS 15.5 (19F70) - DeviceType: iPhone 13 Pro Max
dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode_13.4.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_INSERT_LIBRARIES=/Applications/Xcode_13.4.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/DVTInstrumentsFoundation.framework/liboainject.dylib
abort() called
Kernel Triage:
VM - Compressor failed a blocking pager_get
VM - Compressor failed a blocking pager_get
VM - Compressor failed a blocking pager_get
VM - Compressor failed a blocking pager_get
There is not much information, but I can guess what the problem is
Any Api call asynchronous, and you need use #escaping completion for handle result of dataTask
private func getTestDetailSuccessResponse(completion: #escaping (TestDetailResponseModel>)->Void) {
var testDetailResponseModel: TestDetailResponseModel?
// Make api call
// return result of call into completion as "completion(result)"
}
Using:
private func loadDetail() {
getTestDetailSuccessResponse { result in
let detail = result?.pageLoad
}
}
I have the following base NSManagedObject in our Application:
#objc(LaravelEntity)
class LaravelEntity: NSManagedObject {
static func create(moc: NSManagedObjectContext) -> LaravelEntity? {
let entityName = NSStringFromClass(self)
let ent = NSEntityDescription.insertNewObject(forEntityName: entityName, into: moc) as? LaravelEntity
// We set local changes to true, so it will definitely be pushed to the server
ent?.local_changes = true
return ent
}
static func lastUpdatedRequest() -> NSFetchRequest<LaravelEntity> {
let request : NSFetchRequest<LaravelEntity> = self.self.fetchRequest()
let sort = NSSortDescriptor(key: #keyPath(LaravelEntity.updated_at), ascending: false)
request.sortDescriptors = [sort]
request.fetchLimit = 1
return request
}
}
I also have some subclasses of this. They are defined as subclasses in the DataModel as well:
#objc(Answer)
class Answer: LaravelEntity {
}
I try to fetch the last updated entity for a given class like this:
func lastUpdated(klass: LaravelEntity.Type, ctx : NSManagedObjectContext? = nil) -> LaravelEntity? {
do {
let request = klass.lastUpdatedRequest()
let result = try ctx?.fetch(request) ?? self.persistentContainer.newBackgroundContext().fetch(request)
return result.first
} catch {
print("Error while fetching last updated: \(error)!")
}
return nil
}
This works great for the most part. However, sporadically the application just crashes when trying to do something as simple as querying the id: lastUpdated?.id == 1. I have no idea why this would crash, as all optionals are safely handled (as far as I can tell). Also, when looking at the objects in the debugger, they display fine (e.g. po lastUpdated?.id == 1 returns true in the debugger).
I have created the following example test case to reproduce the problem, somewhat consistently:
func testLastUpdated() {
for i in 0...1000 {
let cases = [Location.self, Position.self, Answer.self, Question.self]
for kase in cases {
let old = kase.create(moc: sm.persistentContainer.viewContext)
let new = kase.create(moc: sm.persistentContainer.viewContext)
XCTAssert(old != nil, "Couldn't create old \(kase) object!")
XCTAssert(new != nil, "Couldn't create new \(kase) object!")
old?.updated_at = Date(timeIntervalSinceNow: TimeInterval(-60*60*24 + i * 60))
old?.id = Int32(i*2)
new?.updated_at = Date(timeIntervalSinceNow: TimeInterval(i*60))
new?.id = Int32(i*2+1)
try! sm.persistentContainer.viewContext.save()
let last_updated = sm.lastUpdated(klass: kase)
XCTAssert(last_updated?.id == new?.id, "Last updated \(kase) doesn't have the correct id \(last_updated?.id ?? -1) vs \(new?.id ?? -1)!") //It crashes on this line.
}
}
}
However, even with a 1000 repetitions, it only crashes about 1/3 of the time I run the test. Additionally, it only crashes in the low 50s of iterations.
The following is the callstack when running the above test:
#0 0x00000001857f8430 in objc_msgSend ()
#1 0x0000000188e1f274 in -[NSPersistentStoreCoordinator _canRouteToStore:forContext:] ()
#2 0x0000000188e21f38 in __110-[NSPersistentStoreCoordinator(_NSInternalMethods) newValueForRelationship:forObjectWithID:withContext:error:]_block_invoke ()
#3 0x0000000188e29af0 in gutsOfBlockToNSPersistentStoreCoordinatorPerform ()
#4 0x0000000185f19048 in _dispatch_client_callout ()
#5 0x0000000185f5b760 in _dispatch_sync_invoke_and_complete_recurse ()
#6 0x0000000185f5b26c in _dispatch_sync_wait ()
#7 0x0000000188e17f74 in _perform ()
#8 0x0000000188e17d2c in -[NSPersistentStoreCoordinator _routeLightweightBlock:toStore:] ()
#9 0x0000000188d4bc94 in -[NSPersistentStoreCoordinator(_NSInternalMethods) newValueForRelationship:forObjectWithID:withContext:error:] ()
#10 0x0000000188d34d7c in _PFFaultHandlerFulfillFault ()
#11 0x0000000188d32a80 in _PFFaultHandlerLookupRow ()
#12 0x0000000188d32334 in _PF_FulfillDeferredFault ()
#13 0x0000000188dd7644 in _pvfk_header ()
#14 0x0000000188dd7450 in _sharedIMPL_pvfk_core_i ()
#15 0x00000001092484a4 in implicit closure #5 in SyncManagerTests.testLastUpdated() at CDTests.swift:74
#16 0x00000001092589d4 in partial apply for implicit closure #5 in SyncManagerTests.testLastUpdated() ()
#17 0x00000001092a5bdc in partial apply for closure #1 in XCTAssertTrue(_:_:file:line:) ()
#18 0x00000001092a5448 in partial apply for closure #1 in _XCTRunThrowableBlock(_:) ()
#19 0x0000000109290224 in thunk for #callee_owned () -> () ()
#20 0x00000001092a6048 in _XCTRunThrowableBlockBridge ()
#21 0x00000001092943bc in specialized _XCTRunThrowableBlock(_:) ()
#22 0x0000000109296118 in specialized XCTAssertTrue(_:_:file:line:) ()
#23 0x000000010929045c in XCTAssert(_:_:file:line:) ()
#24 0x0000000109248010 in SyncManagerTests.testLastUpdated() at CDTests.swift:74
#25 0x0000000109248aa8 in #objc SyncManagerTests.testLastUpdated() ()
#26 0x000000018659d670 in __invoking___ ()
#27 0x000000018647c6cc in -[NSInvocation invoke] ()
#28 0x0000000107996654 in __24-[XCTestCase invokeTest]_block_invoke.275 ()
#29 0x00000001079e4208 in -[XCTMemoryChecker _assertInvalidObjectsDeallocatedAfterScope:] ()
#30 0x0000000107996404 in __24-[XCTestCase invokeTest]_block_invoke ()
#31 0x00000001079dc9d8 in -[XCUITestContext performInScope:] ()
#32 0x000000010799614c in -[XCTestCase invokeTest] ()
#33 0x0000000107997224 in __26-[XCTestCase performTest:]_block_invoke.382 ()
#34 0x00000001079e1a78 in +[XCTContext runInContextForTestCase:block:] ()
#35 0x0000000107996c20 in -[XCTestCase performTest:] ()
#36 0x0000000107992e14 in __27-[XCTestSuite performTest:]_block_invoke ()
#37 0x000000010799283c in -[XCTestSuite _performProtectedSectionForTest:testSection:] ()
#38 0x0000000107992a4c in -[XCTestSuite performTest:] ()
#39 0x0000000107992e14 in __27-[XCTestSuite performTest:]_block_invoke ()
#40 0x000000010799283c in -[XCTestSuite _performProtectedSectionForTest:testSection:] ()
#41 0x0000000107992a4c in -[XCTestSuite performTest:] ()
#42 0x0000000107992e14 in __27-[XCTestSuite performTest:]_block_invoke ()
#43 0x000000010799283c in -[XCTestSuite _performProtectedSectionForTest:testSection:] ()
#44 0x0000000107992a4c in -[XCTestSuite performTest:] ()
#45 0x00000001079eb484 in __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke ()
#46 0x00000001079a5994 in -[XCTestObservationCenter _observeTestExecutionForBlock:] ()
#47 0x00000001079eb300 in -[XCTTestRunSession runTestsAndReturnError:] ()
#48 0x00000001079823d4 in -[XCTestDriver runTestsAndReturnError:] ()
#49 0x00000001079e0c20 in _XCTestMain ()
#50 0x000000018653e0fc in __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ ()
#51 0x000000018653d9cc in __CFRunLoopDoBlocks ()
#52 0x000000018653b6dc in __CFRunLoopRun ()
#53 0x000000018645bfb8 in CFRunLoopRunSpecific ()
#54 0x00000001882f3f84 in GSEventRunModal ()
#55 0x000000018fa302e8 in UIApplicationMain ()
#56 0x000000010137f528 in main at AppDelegate.swift:13
#57 0x0000000185f7e56c in start ()
I have no idea what could cause such a crash, since all optionals are handled correctly.
Edit 1: So the crash definitely happens on the following line last_updated?.id == new?.id (Which still doesn't make any sense to me). Even if I comment out all code changing the objects and only create one object, it still crashes (randomly) on lastUpdated?.id != nil.
Core data is not thread safe. Every context has one and only one thread that is can be read or written from. If you violate this, the behavior is UNDEFINED. Which means that it may crash or it may not. It may wait for 10 minutes and then crash when you do something unrelated.
In your code you are creating a newBackgroundContext inside func lastUpdated (because you are passing a nil context). Doing a fetch with this context is illegal. You can only use this context with performBlock or performBlockAndWait.
Also you have an extra self. in lastUpdatedRequest
I'm trying to create a custom chat toolbar to display a text view with the message, along with buttons to allow photo selection. I reworked my code this morning to very closely follow this WWDC 2017 talk on text input. The toolbar itself is a UIInputViewController, which uses a custom view to define it's inputView. When I select a button, I present a UIImagePickerController, which works fine. The problem occurs when I choose an image and return back to the conversation screen... at this point when I interact with the toolbar again, it disappears on touch. Here is the stack trace right before if is removed from screen:
#0 0x000000010d3b91fa in ConvoViewController.textViewDidEndEditing(UITextView) -> () at /Users/Joel/Documents/Software Projects/AtMe/AtMe/ConvoViewController.swift:480
#1 0x000000010d3b93ca in #objc ConvoViewController.textViewDidEndEditing(UITextView) -> () ()
#2 0x000000010f4b88b8 in -[UITextView resignFirstResponder] ()
#3 0x000000010ea9cc74 in -[UIView(Hierarchy) _removeFirstResponderFromSubtree] ()
#4 0x000000010ea9d2eb in __UIViewWillBeRemovedFromSuperview ()
#5 0x000000010ea9d09e in -[UIView(Hierarchy) removeFromSuperview] ()
#6 0x000000010f45cf5c in __55-[UIInputWindowController invalidateInputAccessoryView]_block_invoke ()
#7 0x000000010ec069a5 in -[UIResponder _preserveResponderOverridesWhilePerforming:] ()
#8 0x000000010f45ced4 in -[UIInputWindowController invalidateInputAccessoryView] ()
#9 0x000000010f45d889 in -[UIInputWindowController changeToInputViewSet:] ()
#10 0x000000010f456675 in -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] ()
#11 0x000000010f45e82f in -[UIInputWindowController setInputViewSet:] ()
#12 0x000000010f45614c in -[UIInputWindowController performOperations:withAnimationStyle:] ()
#13 0x000000010f0cca8a in -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] ()
#14 0x000000010ec0962d in -[UIResponder(UIResponderInputViewAdditions) reloadInputViews] ()
#15 0x000000010ec07ef1 in -[UIResponder _windowBecameKey] ()
#16 0x000000010ea6e9bb in -[UIWindow _makeKeyWindowIgnoringOldKeyWindow:] ()
#17 0x000000010ef33ab4 in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] ()
#18 0x000000010ef37178 in -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] ()
#19 0x000000010ef24f59 in -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] ()
#20 0x000000010ef2cd57 in _UIGestureRecognizerSendTargetActions ()
#21 0x000000010ef2a70b in _UIGestureRecognizerSendActions ()
#22 0x000000010ef299ce in -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] ()
#23 0x000000010ef16152 in _UIGestureEnvironmentUpdate ()
#24 0x000000010ef15c43 in -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] ()
#25 0x000000010ef14e0a in -[UIGestureEnvironment _updateGesturesForEvent:window:] ()
#26 0x000000010ea60eea in -[UIWindow sendEvent:] ()
#27 0x000000010ea0da84 in -[UIApplication sendEvent:] ()
#28 0x000000010f1f15d4 in __dispatchPreprocessedEventFromEventQueue ()
#29 0x000000010f1e9532 in __handleEventQueue ()
#30 0x0000000110d14c01 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#31 0x0000000110cfa0cf in __CFRunLoopDoSources0 ()
#32 0x0000000110cf95ff in __CFRunLoopRun ()
#33 0x0000000110cf9016 in CFRunLoopRunSpecific ()
#34 0x00000001135fba24 in GSEventRunModal ()
#35 0x000000010e9f0134 in UIApplicationMain ()
#36 0x000000010d3ebed7 in main at /Users/Joel/Documents/Software Projects/AtMe/AtMe/AppDelegate.swift:15
#37 0x000000011258065d in start ()
#38 0x000000011258065d in start ()
I get the feeling that the UIInputViewController is invalidated in some way, but I don't know how to test this. Does anyone have any ideas? I have spent multiple days trying to make this toolbar work bug free, and it seems impossible no matter how you approach it.
conversationViewController.swift
// MARK: - Input Accessory View (Controller)
fileprivate let chatToolbarView: ChatToolbarView = {
let view = ChatToolbarView(frame: CGRect.zero, inputViewStyle: UIInputViewStyle.default)
// Add selectors as targets to the toolbar buttons
view.sendButton.addTarget(self, action: #selector(didPressSend(sender:)), for: UIControlEvents.touchUpInside)
view.libraryButton.addTarget(self, action: #selector(didPressLibraryIcon(sender:)), for: UIControlEvents.touchUpInside)
view.cameraButton.addTarget(self, action: #selector(didPressCameraIcon(sender:)), for: UIControlEvents.touchUpInside)
return view
}()
// Wrapper view controller for the custom input accessory view
fileprivate let chatToolbarViewController = UIInputViewController()
override var inputAccessoryViewController: UIInputViewController? {
// Ensure our input accessory view controller has it's input view set
chatToolbarViewController.inputView = chatToolbarView
// Return our custom input accessory view controller. You could also just return a UIView with
// override func inputAccessoryView()
return chatToolbarViewController
}
/** Action method which fires when the user taps the camera icon. */
#objc func didPressCameraIcon(sender: Any) {
photoPickerDisplayed = true
// Create picker, and set this controller as delegate
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = UIImagePickerControllerSourceType.camera
DispatchQueue.main.async {
self.present(picker, animated: true, completion: nil)
}
}
override var canBecomeFirstResponder: Bool { return true }
I ended up solving the issue by assigning my custom toolbar view to the view controller's inputAccessoryView, as opposed to assigning it to the inputView property of the view controller's inputAccessoryViewController.
The big issue was that with the original code, any view controllers presented modally (eg. UIImagePickerViewController) confused the inputAccessoryViewController, and would be dismissed entirely upon return to the original controller. Using the controller seemed to prove unnecessary anyways, it is more for direct keyboard customization as opposed to accessories.
The code now looks like this:
fileprivate let chatToolbarView: ChatToolbarView = {
let view = ChatToolbarView(frame: CGRect.zero, inputViewStyle: UIInputViewStyle.default)
// Add selectors as targets to the toolbar buttons
view.sendButton.addTarget(self, action: #selector(didPressSend(sender:)), for: UIControlEvents.touchUpInside)
view.libraryButton.addTarget(self, action: #selector(didPressLibraryIcon(sender:)), for: UIControlEvents.touchUpInside)
view.cameraButton.addTarget(self, action: #selector(didPressCameraIcon(sender:)), for: UIControlEvents.touchUpInside)
return view
}()
/** Provide the view controller's inputAccessoryView object. */
override var inputAccessoryView: UIView? { return chatToolbarView }
/** Give the view controller permission to become first responder. */
override var canBecomeFirstResponder: Bool { return true }
On you viewDidAppear
yourTextView.becomeFirstResponder()
This will trigger your custom inputView to open again.
Hope this helps!
I'm getting a crash from GCDWebServer (3.3.3) when my app enters the background:
#3 0x000000010041ea80 in -[GCDWebServer dealloc] at project/Pods/GCDWebServer/GCDWebServer/Core/GCDWebServer.m:221
#4 0x00000001004248b8 in __destroy_helper_block_ ()
#5 0x000000018dd52a28 in _Block_release ()
#6 0x00000001020ad21c in _dispatch_client_callout ()
#7 0x00000001020b2284 in _dispatch_main_queue_callback_4CF ()
#8 0x000000018ee21f2c in __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ ()
#9 0x000000018ee1fb18 in __CFRunLoopRun ()
#10 0x000000018ed4e048 in CFRunLoopRunSpecific ()
#11 0x00000001907d1198 in GSEventRunModal ()
#12 0x0000000194d28628 in -[UIApplication _run] ()
#13 0x0000000194d23360 in UIApplicationMain ()
#14 0x000000010009243c in main at project/main.m:10
#15 0x000000018dd305b8 in start ()
Enqueued from com.apple.main-thread (Thread 1)Queue : com.apple.main-thread (serial)
#0 0x00000001020b8ba4 in _dispatch_queue_push ()
#1 0x0000000100424680 in -[GCDWebServer _stop] at project/Pods/GCDWebServer/GCDWebServer/Core/GCDWebServer.m:734
#2 0x0000000100424a10 in -[GCDWebServer _didEnterBackground:] at project/Pods/GCDWebServer/GCDWebServer/Core/GCDWebServer.m:746
The specific line is:
GWS_DCHECK(_options == nil); // The server can never be dealloc'ed while running because of the retain-cycle with the dispatch source
It looks like the _options dictionary has to be nil (eg. the server has to be stopped) but it looks like the _options is never set to nil on this code path: it's set to nil in - stop but not in - _stop.
I'm probably missing something as this would have been noticed by other people.
I had the same problem. I solved it saving the server in a static variable on my class instead of saving in a function.
It doesn't works:
class Server {
static func initialize() {
let webServer = GCDWebServer()
...
webServer?.start(withPort: 8081, bonjourName: nil)
}
}
It's works:
class Server {
static let webServer = GCDWebServer()
static func initialize() {
...
webServer?.start(withPort: 8081, bonjourName: nil)
}
}