Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index out of bounds error after delete when using @ObservedSectionedResults in SwiftUI #8294

Closed
GriffinMeyer opened this issue Jul 7, 2023 · 8 comments

Comments

@GriffinMeyer
Copy link

How frequently does the bug occur?

Always

Description

When using ObservedSectionedResults in SwiftUI, if you delete an item within a section, the app will crash with an index out of bounds error.

Stacktrace & log output

2023-07-07 16:59:30.123938-0500 realm-bug-example[6128:92124] *** Terminating app due to uncaught exception 'RLMException', reason: 'Index 2 is out of bounds (must be less than 2).'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000180437330 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x0000000180051274 objc_exception_throw + 56
	2   realm-bug-example                   0x0000000104677318 _Z27RLMThrowCollectionExceptionP8NSString + 248
	3   realm-bug-example                   0x0000000104696d20 _ZL24translateCollectionErrorIZ28-[RLMSection objectAtIndex:]E3$_5EDaOT_P8NSString + 120
	4   realm-bug-example                   0x0000000104690d60 -[RLMSection objectAtIndex:] + 136
	5   realm-bug-example                   0x0000000104690ccc -[RLMSection objectAtIndexedSubscript:] + 40
	6   realm-bug-example                   0x00000001047daa3c $s10RealmSwift14ResultsSectionVyq_Sicig + 272
	7   realm-bug-example                   0x00000001047dc428 $s10RealmSwift14ResultsSectionVyq_Sicir + 156
	8   realm-bug-example                   0x00000001047dc310 $s10RealmSwift14ResultsSectionVyxq_GSlAASly7ElementQz5IndexQzcirTW + 92
	9   SwiftUI                             0x000000010d0e0450 OUTLINED_FUNCTION_61 + 944
	10  SwiftUI                             0x000000010d0e8494 OUTLINED_FUNCTION_61 + 33780
	11  SwiftUI                             0x000000010d0e8c80 OUTLINED_FUNCTION_61 + 35808
	12  SwiftUI                             0x000000010d0e8be8 OUTLINED_FUNCTION_61 + 35656
	13  SwiftUI                             0x000000010d1cf064 OUTLINED_FUNCTION_34 + 21608
	14  SwiftUI                             0x000000010cff5418 OUTLINED_FUNCTION_9 + 7060
	15  SwiftUI                             0x000000010cff5888 OUTLINED_FUNCTION_9 + 8196
	16  SwiftUI                             0x000000010d265f50 OUTLINED_FUNCTION_3 + 20752
	17  SwiftUI                             0x000000010d266120 OUTLINED_FUNCTION_3 + 21216
	18  SwiftUI                             0x000000010d266040 OUTLINED_FUNCTION_3 + 20992
	19  SwiftUI                             0x000000010c7506fc OUTLINED_FUNCTION_1 + 5468
	20  SwiftUI                             0x000000010c75095c OUTLINED_FUNCTION_1 + 6076
	21  SwiftUI                             0x000000010c7508a4 OUTLINED_FUNCTION_1 + 5892
	22  libswiftCore.dylib                  0x000000018bd0d7f0 $ss16IndexingIteratorVyxGStsSt4next7ElementQzSgyFTW + 356
	23  libswiftCore.dylib                  0x000000018be02250 $sSTsE21_copySequenceContents12initializing8IteratorQz_SitSry7ElementQzG_tF + 368
	24  libswiftCore.dylib                  0x000000018bcca4a8 $ss32_copyCollectionToContiguousArrayys0dE0Vy7ElementQzGxSlRzlF + 516
	25  libswiftCore.dylib                  0x000000018bcbacd4 $sSTsE22_copyToContiguousArrays0cD0Vy7ElementQzGyFTm + 40
	26  libswiftCore.dylib                  0x000000018bcbfd8c $ss15ContiguousArrayVyAByxGqd__c7ElementQyd__RszSTRd__lufC + 32
	27  SwiftUI                             0x000000010c750098 OUTLINED_FUNCTION_1 + 3832
	28  SwiftUI                             0x000000010c916680 OUTLINED_FUNCTION_35 + 4584
	29  SwiftUI                             0x000000010c91602c OUTLINED_FUNCTION_35 + 2964
	30  SwiftUI                             0x000000010c915594 OUTLINED_FUNCTION_35 + 252
	31  SwiftUI                             0x000000010d1e13c4 OUTLINED_FUNCTION_2 + 33020
	32  SwiftUI                             0x000000010d1ddc6c OUTLINED_FUNCTION_2 + 18852
	33  SwiftUI                             0x000000010d1da604 OUTLINED_FUNCTION_2 + 4924
	34  SwiftUI                             0x000000010d1d9d04 OUTLINED_FUNCTION_2 + 2620
	35  SwiftUI                             0x000000010cddfa58 OUTLINED_FUNCTION_11 + 27760
	36  SwiftUI                             0x000000010d32a178 OUTLINED_FUNCTION_26 + 616
	37  SwiftUI                             0x000000010c6da518 OUTLINED_FUNCTION_80 + 30888
	38  SwiftUI                             0x000000010c62ba00 OUTLINED_FUNCTION_5 + 26808
	39  AttributeGraph                      0x000000019d23fe34 _ZN2AG5Graph11UpdateStack6updateEv + 512
	40  AttributeGraph                      0x000000019d240534 _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 428
	41  AttributeGraph                      0x000000019d24d7a4 _ZN2AG8Subgraph6updateEj + 844
	42  SwiftUI                             0x000000010d304c40 OUTLINED_FUNCTION_37 + 1896
	43  SwiftUI                             0x000000010d307090 OUTLINED_FUNCTION_37 + 11192
	44  SwiftUI                             0x000000010ca00cf0 OUTLINED_FUNCTION_0 + 1716
	45  SwiftUI                             0x000000010d17f88c OUTLINED_FUNCTION_58 + 28272
	46  SwiftUI                             0x000000010d179c54 OUTLINED_FUNCTION_58 + 4664
	47  SwiftUI                             0x000000010ca00bcc OUTLINED_FUNCTION_0 + 1424
	48  SwiftUI                             0x000000010d30705c OUTLINED_FUNCTION_37 + 11140
	49  SwiftUI                             0x000000010d306f94 OUTLINED_FUNCTION_37 + 10940
	50  SwiftUI                             0x000000010ccc4e3c OUTLINED_FUNCTION_16 + 4404
	51  SwiftUI                             0x000000010c754110 OUTLINED_FUNCTION_1 + 20336
	52  SwiftUI                             0x000000010c754090 OUTLINED_FUNCTION_1 + 20208
	53  SwiftUI                             0x000000010c754188 OUTLINED_FUNCTION_1 + 20456
	54  CoreFoundation                      0x0000000180399c10 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
	55  CoreFoundation                      0x000000018039457c __CFRunLoopDoObservers + 512
	56  CoreFoundation                      0x0000000180394a20 __CFRunLoopRun + 948
	57  CoreFoundation                      0x0000000180394254 CFRunLoopRunSpecific + 584
	58  GraphicsServices                    0x0000000188eb7c9c GSEventRunModal + 160
	59  UIKitCore                           0x0000000109152ff0 -[UIApplication _run] + 868
	60  UIKitCore                           0x0000000109156f3c UIApplicationMain + 124
	61  SwiftUI                             0x000000010d1ff34c OUTLINED_FUNCTION_54 + 500
	62  SwiftUI                             0x000000010d1ff1ec OUTLINED_FUNCTION_54 + 148
	63  SwiftUI                             0x000000010c9f7474 OUTLINED_FUNCTION_16 + 88
	64  realm-bug-example                   0x00000001043e4318 $s17realm_bug_example0a1_b1_C3AppV5$mainyyFZ + 40
	65  realm-bug-example                   0x00000001043e43c4 main + 12
	66  dyld                                0x0000000107f81514 start_sim + 20
	67  ???                                 0x0000000108085f28 0x0 + 4429733672
	68  ???                                 0x2549000000000000 0x0 + 2686678652703211520
)
libc++abi: terminating due to uncaught exception of type NSException

Can you reproduce the bug?

Always

Reproduction Steps

I've created a repo here: https://github.com/GriffinMeyer/section-list-delete-bug
Simply building and running the app, adding multiple Todos and then swiping to delete will cause the app to crash.
This also happens when using a synced realm, and the object is deleted from another source, so the issue is not specifically with the delete code.

Version

10.41.0

What Atlas Services are you using?

Atlas Device Sync

Are you using encryption?

No

Platform OS and version(s)

iOS 16.4, iOS 17

Build environment

Xcode version: 14.3.1
Dependency manager and version: SPM

@leemaguire
Copy link
Contributor

Hi @GriffinMeyer Thanks for the reproduction app! I'm investigating this now.

@leemaguire
Copy link
Contributor

leemaguire commented Jul 10, 2023

@GriffinMeyer could you check this PR out and see if it solves your issue?

You will now need to use the @ObservedSectionedResults API for deletions like so:

.onDelete {
    $todos.remove(atOffsets: $0, section: section)
}

@GriffinMeyer
Copy link
Author

@leemaguire Yes! Problem fixed, and the deletions API is much cleaner!

@sync-by-unito sync-by-unito bot removed the T-Bug label Sep 12, 2023
@GriffinMeyer
Copy link
Author

Any updates on getting this merged?

@YevhenPryliepov
Copy link

@leemaguire
Any news?

@leemaguire
Copy link
Contributor

@YevhenPryliepov looking into it now.

@leemaguire
Copy link
Contributor

Fixed in #8295

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants