Skip to content

macosx: Fix crash when adding media to groups

Adding or deleting new media files to folders in the media library (e.g. adding episodes to a TV series) causes a crash with the following exception due to collection view item count mismatches.

This was due to calling self.collectionView reloadItemsAtIndexPaths: in libraryModelGroupUpdated: which crashed because in the case of adding or deleting media items, the count changed.

I suppose the direct fix would be to use self.collectionView reloadData but my approach is to extend VLCLibraryModelGroupUpdated into three notifications: VLCLibraryModelGroupMediaItemAdded, VLCLibraryModelGroupMediaItemRemoved, and VLCLibraryModelGroupMetadataUpdated. This way we can use self.collectionView reloadData for add and remove changes and preserve self.collectionView reloadItemsAtIndexPaths: for metadata updates.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0 (section 0 before update). The number of items contained in an existing section after the update (3) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (1 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). 0 inserts and 0 deletes came from client insert and delete calls. 1 rows in this section were reloaded. Before update: 1 sections. After: 1 sections.'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000181653ae0 __exceptionPreprocess + 176
	1   libobjc.A.dylib                     0x0000000181116b90 objc_exception_throw + 88
	2   Foundation                          0x0000000182c63a78 -[NSCalendarDate initWithCoder:] + 0
	3   AppKit                              0x00
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0 (section 0 before update). The number of items contained in an existing section after the update (3) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (1 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). 0 inserts and 0 deletes came from client insert and delete calls. 1 rows in this section were reloaded. Before update: 1 sections. After: 1 sections.'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000181653ae0 __exceptionPreprocess + 176
	1   libobjc.A.dylib                     0x0000000181116b90 objc_exception_throw + 88
	2   Foundation                          0x0000000182c63a78 -[NSCalendarDate initWithCoder:] + 0
	3   AppKit                              0x000000018600eec0 __101-[_NSCollectionViewCore _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:]_block_invoke + 160
	4   AppKit                              0x000000018600e510 -[_NSCollectionViewCore _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 8820
	5   AppKit                              0x000000018600c0c0 -[_NSCollectionViewCore _updateRowsAtIndexPaths:updateAction:] + 244
	6   AppKit                              0x00000001861569d4 -[NSCollectionView reloadItemsAtIndexPaths:] + 176
	7   libmacosx_plugin.dylib              0x0000000100ac0430 -[VLCLibraryGroupsDataSource libraryModelGroupUpdated:] + 180
	8   CoreFoundation                      0x00000001815d746c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 148
	9   CoreFoundation                      0x0000000181666b28 ___CFXRegistrationPost_block_invoke + 92
	10  CoreFoundation                      0x0000000181666a6c _CFXRegistrationPost + 436
	11  CoreFoundation                      0x00000001815a68b8 _CFXNotificationPost + 740
	12  Foundation                          0x0000000182b60680 -[NSNotificationCenter postNotificationName:object:userInfo:] + 88
	13  libmacosx_plugin.dylib              0x0000000100a97c64 __42-[VLCLibraryModel handleGroupUpdateEvent:]_block_invoke.173 + 204
	14  libdispatch.dylib                   0x000000018135985c _dispatch_client_callout + 16
	15  libdispatch.dylib                   0x000000018134f880 _dispatch_async_and_wait_invoke + 84
	16  libdispatch.dylib                   0x000000018135985c _dispatch_client_callout + 16
	17  libdispatch.dylib                   0x0000000181376b80 _dispatch_main_queue_drain.cold.5 + 812
	18  libdispatch.dylib                   0x000000018134edb0 _dispatch_main_queue_drain + 180
	19  libdispatch.dylib                   0x000000018134ecec _dispatch_main_queue_callback_4CF + 44
	20  CoreFoundation                      0x0000000181620be0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
	21  CoreFoundation                      0x00000001815e18dc __CFRunLoopRun + 1980
	22  CoreFoundation                      0x00000001815e0a98 CFRunLoopRunSpecific + 572
	23  HIToolbox                           0x000000018d08327c RunCurrentEventLoopInMode + 324
	24  HIToolbox                           0x000000018d0864e8 ReceiveNextEventCommon + 676
	25  HIToolbox                           0x000000018d211484 _BlockUntilNextEventMatchingListInModeWithFilter + 76
	26  AppKit                              0x0000000185505a34 _DPSNextEvent + 684
	27  AppKit                              0x0000000185ea4940 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 688
	28  AppKit                              0x00000001854f8be4 -[NSApplication run] + 480
	29  libmacosx_plugin.dylib              0x0000000100b0c5f0 __OpenIntf_block_invoke + 868
	30  CoreFoundation                      0x00000001815e26b4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28
	31  CoreFoundation                      0x00000001815e25c4 __CFRunLoopDoBlocks + 352
	32  CoreFoundation                      0x00000001815e1a70 __CFRunLoopRun + 2384
	33  CoreFoundation                      0x00000001815e0a98 CFRunLoopRunSpecific + 572
	34  CoreFoundation                      0x000000018165a554 CFRunLoopRun + 64
	35  vlc-osx-static                      0x0000000100002d30 main + 1824
	36  dyld                                0x0000000181156b98 start + 6076
)
2025-08-27 15:37:32.922692+0300 vlc-osx-static[11382:158957] [General] An uncaught exception was raised
2025-08-27 15:37:32.923100+0300 vlc-osx-static[11382:158957] [General] Invalid update: invalid number of items in section 0 (section 0 before update). The number of items contained in an existing section after the update (3) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (1 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). 0 inserts and 0 deletes came from client insert and delete calls. 1 rows in this section were reloaded. Before update: 1 sections. After: 1 sections.
2025-08-27 15:37:32.923149+0300 vlc-osx-static[11382:158957] [General] (
	0   CoreFoundation                      0x0000000181653ae0 __exceptionPreprocess + 176
	1   libobjc.A.dylib                     0x0000000181116b90 objc_exception_throw + 88
	2   Foundation                          0x0000000182c63a78 -[NSCalendarDate initWithCoder:] + 0
	3   AppKit                              0x000000018600eec0 __101-[_NSCollectionViewCore _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:]_block_invoke + 160
	4   AppKit                              0x000000018600e510 -[_NSCollectionViewCore _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 8820
	5   AppKit                              0x000000018600c0c0 -[_NSCollectionViewCore _updateRowsAtIndexPaths:updateAction:] + 244
	6   AppKit                              0x00000001861569d4 -[NSCollectionView reloadItemsAtIndexPaths:] + 176
	7   libmacosx_plugin.dylib              0x0000000100ac0430 -[VLCLibraryGroupsDataSource libraryModelGroupUpdated:] + 180
	8   CoreFoundation                      0x00000001815d746c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 148
	9   CoreFoundation                      0x0000000181666b28 ___CFXRegistrationPost_block_invoke + 92
	10  CoreFoundation                      0x0000000181666a6c _CFXRegistrationPost + 436
	11  CoreFoundation                      0x00000001815a68b8 _CFXNotificationPost + 740
	12  Foundation                          0x0000000182b60680 -[NSNotificationCenter postNotificationName:object:userInfo:] + 88
	13  libmacosx_plugin.dylib              0x0000000100a97c64 __42-[VLCLibraryModel handleGroupUpdateEvent:]_block_invoke.173 + 204
	14  libdispatch.dylib                   0x000000018135985c _dispatch_client_callout + 16
	15  libdispatch.dylib                   0x000000018134f880 _dispatch_async_and_wait_invoke + 84
	16  libdispatch.dylib                   0x000000018135985c _dispatch_client_callout + 16
	17  libdispatch.dylib                   0x0000000181376b80 _dispatch_main_queue_drain.cold.5 + 812
	18  libdispatch.dylib                   0x000000018134edb0 _dispatch_main_queue_drain + 180
	19  libdispatch.dylib                   0x000000018134ecec _dispatch_main_queue_callback_4CF + 44
	20  CoreFoundation                      0x0000000181620be0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
	21  CoreFoundation                      0x00000001815e18dc __CFRunLoopRun + 1980
	22  CoreFoundation                      0x00000001815e0a98 CFRunLoopRunSpecific + 572
	23  HIToolbox                           0x000000018d08327c RunCurrentEventLoopInMode + 324
	24  HIToolbox                           0x000000018d0864e8 ReceiveNextEventCommon + 676
	25  HIToolbox                           0x000000018d211484 _BlockUntilNextEventMatchingListInModeWithFilter + 76
	26  AppKit                              0x0000000185505a34 _DPSNextEvent + 684
	27  AppKit                              0x0000000185ea4940 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 688
	28  AppKit                              0x00000001854f8be4 -[NSApplication run] + 480
	29  libmacosx_plugin.dylib              0x0000000100b0c5f0 __OpenIntf_block_invoke + 868
	30  CoreFoundation                      0x00000001815e26b4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28
	31  CoreFoundation                      0x00000001815e25c4 __CFRunLoopDoBlocks + 352
	32  CoreFoundation                      0x00000001815e1a70 __CFRunLoopRun + 2384
	33  CoreFoundation                      0x00000001815e0a98 CFRunLoopRunSpecific + 572
	34  CoreFoundation                      0x000000018165a554 CFRunLoopRun + 64
	35  vlc-osx-static                      0x0000000100002d30 main + 1824
	36  dyld                                0x0000000181156b98 start + 6076
)
2025-08-27 15:37:32.924644+0300 vlc-osx-static[11382:158957] [HIExceptions] FAULT: NSInternalInconsistencyException: Invalid update: invalid number of items in section 0 (section 0 before update). The number of items contained in an existing section after the update (3) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (1 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). 0 inserts and 0 deletes came from client insert and delete calls. 1 rows in this section were reloaded. Before update: 1 sections. After: 1 sections.; {
    NSAssertFile = "UICollectionView.m";
    NSAssertLine = 4234;
}
libc++abi: terminating due to uncaught exception of type NSException
Process 11382 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001814be388 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`__pthread_kill:
->  0x1814be388 <+8>:  b.lo   0x1814be3a8    ; <+40>
    0x1814be38c <+12>: pacibsp 
    0x1814be390 <+16>: stp    x29, x30, [sp, #-0x10]!
    0x1814be394 <+20>: mov    x29, sp
Target 0: (vlc-osx-static) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
  * frame #0: 0x00000001814be388 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x00000001814f788c libsystem_pthread.dylib`pthread_kill + 296
    frame #2: 0x0000000181400a3c libsystem_c.dylib`abort + 124
    frame #3: 0x00000001814ad384 libc++abi.dylib`abort_message + 132
    frame #4: 0x000000018149bcf4 libc++abi.dylib`demangling_terminate_handler() + 344
    frame #5: 0x0000000181120dd4 libobjc.A.dylib`_objc_terminate() + 156
    frame #6: 0x00000001814ac698 libc++abi.dylib`std::__terminate(void (*)()) + 16
    frame #7: 0x00000001814afc30 libc++abi.dylib`__cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception*) + 88
    frame #8: 0x00000001814afbd8 libc++abi.dylib`__cxa_throw + 92
    frame #9: 0x0000000181116cf8 libobjc.A.dylib`objc_exception_throw + 448
    frame #10: 0x0000000182c63a78 Foundation`-[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 288
    frame #11: 0x000000018600eec0 AppKit`__101-[_NSCollectionViewCore _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:]_block_invoke + 160
    frame #12: 0x000000018600e510 AppKit`-[_NSCollectionViewCore _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 8820
    frame #13: 0x000000018600c0c0 AppKit`-[_NSCollectionViewCore _updateRowsAtIndexPaths:updateAction:] + 244
    frame #14: 0x00000001861569d4 AppKit`-[NSCollectionView reloadItemsAtIndexPaths:] + 176
    frame #15: 0x0000000100ac0430 libmacosx_plugin.dylib`-[VLCLibraryGroupsDataSource libraryModelGroupUpdated:](self=0x00006000032de3a0, _cmd="libraryModelGroupUpdated:", notification=@"VLCLibraryModelGroupUpdated") at VLCLibraryGroupsDataSource.m:84:9
    frame #16: 0x00000001815d746c CoreFoundation`__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 148
    frame #17: 0x0000000181666b28 CoreFoundation`___CFXRegistrationPost_block_invoke + 92
    frame #18: 0x0000000181666a6c CoreFoundation`_CFXRegistrationPost + 436
    frame #19: 0x00000001815a68b8 CoreFoundation`_CFXNotificationPost + 740
    frame #20: 0x0000000182b60680 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 88
    frame #21: 0x0000000100a97c64 libmacosx_plugin.dylib`__42-[VLCLibraryModel handleGroupUpdateEvent:]_block_invoke.173(.block_descriptor=0x000000016fe86800) at VLCLibraryModel.m:1352:13
    frame #22: 0x000000018135985c libdispatch.dylib`_dispatch_client_callout + 16
    frame #23: 0x000000018134f880 libdispatch.dylib`_dispatch_async_and_wait_invoke + 84
    frame #24: 0x000000018135985c libdispatch.dylib`_dispatch_client_callout + 16
    frame #25: 0x0000000181376b80 libdispatch.dylib`_dispatch_main_queue_drain.cold.5 + 812
    frame #26: 0x000000018134edb0 libdispatch.dylib`_dispatch_main_queue_drain + 180
    frame #27: 0x000000018134ecec libdispatch.dylib`_dispatch_main_queue_callback_4CF + 44
    frame #28: 0x0000000181620be0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
    frame #29: 0x00000001815e18dc CoreFoundation`__CFRunLoopRun + 1980
    frame #30: 0x00000001815e0a98 CoreFoundation`CFRunLoopRunSpecific + 572
    frame #31: 0x000000018d08327c HIToolbox`RunCurrentEventLoopInMode + 324
    frame #32: 0x000000018d0864e8 HIToolbox`ReceiveNextEventCommon + 676
    frame #33: 0x000000018d211484 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 76
    frame #34: 0x0000000185505a34 AppKit`_DPSNextEvent + 684
    frame #35: 0x0000000185ea4940 AppKit`-[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 688
    frame #36: 0x00000001854f8be4 AppKit`-[NSApplication run] + 480
    frame #37: 0x0000000100b0c5f0 libmacosx_plugin.dylib`__OpenIntf_block_invoke(.block_descriptor=0x00006000028b5700) at VLCMain.m:196:13
    frame #38: 0x00000001815e26b4 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28
    frame #39: 0x00000001815e25c4 CoreFoundation`__CFRunLoopDoBlocks + 352
    frame #40: 0x00000001815e1a70 CoreFoundation`__CFRunLoopRun + 2384
    frame #41: 0x00000001815e0a98 CoreFoundation`CFRunLoopRunSpecific + 572
    frame #42: 0x000000018165a554 CoreFoundation`CFRunLoopRun + 64
    frame #43: 0x0000000100002d30 vlc-osx-static`main(i_argc=0, ppsz_argv=0x000000016fdff540) at darwinvlc.m:313:9
    frame #44: 0x0000000181156b98 dyld`start + 6076```

Merge request reports

Loading