iOSKit UIAlertController crash

I started using UIAlertController in iOSKit by @Jason King

Unfortunately there are at least two issues:
If an AlertController is displayed, the view’s close event will never fire.
For one of the views using UIAlertController, the view can be closed (close event doesn’t fire), but the next action just after the close crashes the app with the following iOS Simulator report:

[code]Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY

Application Specific Information:
Assertion failed: (RuntimeObjectIsa(obj, mClassPtr), “vAssessmentList.vAssessmentList”), function GetObjectData, file Common/ObjectGlue.h, line 201.

CoreSimulator 307.14.2 - Device: iPad Air 2 - Runtime: iOS 10.1 (14B72) - DeviceType: iPad Air 2

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x03fc749a __pthread_kill + 10
1 libsystem_pthread.dylib 0x040ceec3 pthread_kill + 90
2 libsystem_c.dylib 0x03d5a11f abort + 141
3 libsystem_c.dylib 0x03d24f6a __assert_rtn + 311
4 rbframework.dylib 0x0090b49e 0x8f6000 + 87198
5 rbframework.dylib 0x0090b243 0x8f6000 + 86595
6 rbframework.dylib 0x0090b334 0x8f6000 + 86836
7 rbframework.dylib 0x008f9606 0x8f6000 + 13830
8 rbframework.dylib 0x00a35233 iOSView_AddControl + 68
9 fr.mescarnets.ios1 0x00062c64 iOSView.AddControl%%oo + 47
10 fr.mescarnets.ios1 0x0043740d ControlExtensions.$setTabBarColor%%ocb + 322
11 fr.mescarnets.ios1 0x004370ef ControlExtensions.$SetupView%%ob + 174
12 fr.mescarnets.ios1 0x0021200a vStudentList.vStudentList.Event_Activate%%o<vStudentList.vStudentList> + 310
13 rbframework.dylib 0x00a313f6 0x8f6000 + 1291254
14 rbframework.dylib 0x00a04b23 0x8f6000 + 1108771
15 fr.mescarnets.ios1 0x0007b737 Delegate.Invoke%%p + 48
16 fr.mescarnets.ios1 0x00005bd0 xojo._CallFunctionWithExceptionHandling%o%pp + 211
17 rbframework.dylib 0x00a04ad4 0x8f6000 + 1108692
18 rbframework.dylib 0x00a31e23 0x8f6000 + 1293859
19 com.apple.UIKit 0x00d3e99c -[UIViewController _setViewAppearState:isAnimating:] + 681
20 com.apple.UIKit 0x00d3f149 -[UIViewController __viewWillAppear:] + 147
21 com.apple.UIKit 0x00d62af9 -[UIViewController(UIContainerViewControllerProtectedMethods) beginAppearanceTransition:animated:] + 204
22 com.apple.UIKit 0x00d6e783 -[UINavigationController _startCustomTransition:] + 1438
23 com.apple.UIKit 0x00d80c25 -[UINavigationController _startDeferredTransitionIfNeeded:] + 836
24 com.apple.UIKit 0x00d820e5 -[UINavigationController __viewWillLayoutSubviews] + 70
25 com.apple.UIKit 0x00fa589e -[UILayoutContainerView layoutSubviews] + 232
26 com.apple.UIKit 0x00c40374 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1457
27 libobjc.A.dylib 0x022e71b9 -[NSObject performSelector:withObject:] + 59
28 com.apple.QuartzCore 0x03a2b74d -[CALayer layoutSublayers] + 141
29 com.apple.QuartzCore 0x03a1ea2b CA::Layer::layout_if_needed(CA::Transaction*) + 401
30 com.apple.QuartzCore 0x03a1e881 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 21
31 com.apple.QuartzCore 0x039a84c7 CA::Context::commit_transaction(CA::Transaction*) + 339
32 com.apple.QuartzCore 0x039d72b8 CA::Transaction::commit() + 498
33 com.apple.QuartzCore 0x039d8dc8 CA::Transaction::flush_transaction() + 38
34 com.apple.UIKit 0x00b9a44c _afterCACommitHandler + 375
35 com.apple.CoreFoundation 0x034e176e CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 30
36 com.apple.CoreFoundation 0x034e16c7 __CFRunLoopDoObservers + 391
37 com.apple.CoreFoundation 0x034c53a6 __CFRunLoopRun + 1414
38 com.apple.CoreFoundation 0x034c4bab CFRunLoopRunSpecific + 395
39 com.apple.CoreFoundation 0x034c4a0b CFRunLoopRunInMode + 123
40 com.apple.GraphicsServices 0x05a02b4c GSEventRunModal + 177
41 com.apple.GraphicsServices 0x05a029c7 GSEventRun + 80
42 com.apple.UIKit 0x00b6b32b UIApplicationMain + 148
43 rbframework.dylib 0x009560ae 0x8f6000 + 393390
44 fr.mescarnets.ios1 0x00005def xojo._RuntimeRun + 28
45 fr.mescarnets.ios1 0x00730620 _Main + 258
46 fr.mescarnets.ios1 0x0072b7cb main + 36
47 libdyld.dylib 0x03cb4799 start + 1
[/code]

This is the code to create the UIAlertController:

[code]using UIKit
Dim alertStyle as UIAlertController.UIAlertControllerStyle
if isIPad then
alertStyle = UIAlertController.UIAlertControllerStyle.Alert

else
alertStyle = UIAlertController.UIAlertControllerStyle.ActionSheet
end if
alertController = UIAlertController.AlertControllerWithTitleMessagePreferredStyle(localFilter, localSelectItemsToDisplay, alertStyle)

dim alert as UIAlertAction = UIAlertAction.ActionWithTitleStyleHandler(“All Items”, UIAlertAction.UIAlertActionStyle.Default, AddressOf alertHandler)
alertController.AddAction alert

alert = UIAlertAction.ActionWithTitleStyleHandler(“Cancel”, UIAlertAction.UIAlertActionStyle.Cancel, AddressOf alertHandler)
alertController.AddAction alert

alertController.PresentInView(self)[/code]

And the code in alertHandler :

[code] Select case sender.title

	Case "Cancel"
			
			alertController.Dismiss()
			Return
			
	Case "All Items"
			tableFilter = "all"
			
			
			
	Else
			Break
			//unknown button
			
	End Select
	
	Reload()
	
	alertController.Dismiss[/code]

Any suggestions ?

Hi Jeremie can you send me a PM with the project or a smaller version which demonstrates the issue so I can get this fixed?

Jason

Hi Jason,

Thanks, sending the PM right now.

To anyone else coming across this problem in the future, the issue is the use of AddressOf instead of WeakAddressOf. The offending lines were those like the following

alert = UIAlertAction.ActionWithTitleStyleHandler("Cancel", UIAlertAction.UIAlertActionStyle.Cancel, AddressOf alertHandler)

Which simply needs to be changed to

alert = UIAlertAction.ActionWithTitleStyleHandler("Cancel", UIAlertAction.UIAlertActionStyle.Cancel, WeakAddressOf alertHandler)

So that no reference to the calling view is maintained after the alert is dismissed.

Thank you Jason !