Bug? CallSub2 crash in debug mode

Alessandro71

Well-Known Member
Licensed User
Longtime User
as a premise, this code works fine in B4A release mode.
now i'm porting it to B4i, working in debug mode.

this sub performs some networking setup and then waits for an event
B4X:
Private Sub OpenRouting As ResumableSub
    ...
    Wait For Routing_Complete (success As Boolean)
    ...
End Sub

the event is sent from another sub
B4X:
Public Sub ProcessDoIPMessage(HostAddress As String, m As DoIPMessage)
    ...
    CallSub2(Me, "Routing_Complete", True)
    ...
End Sub

when i run the project, it crashes with the following error (line 219 is the CallSub2(Me, "Routing_Complete", True) line)
Rich (BB code):
Error occurred on line: 219 (DoIPLink)
*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]
Stack Trace: (
  CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 1135176
  libobjc.A.dylib      objc_exception_throw + 88
  CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 147916
  DoIP Test Client     -[ResumableSub_doiplink_OpenRouting resume::] + 1172
  CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 407796
  CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 407416
  DoIP Test Client     +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 776
  DoIP Test Client     -[B4IShell runMethod:] + 320
  DoIP Test Client     -[B4IShell raiseEventImpl:method:args::] + 932
  DoIP Test Client     -[B4IShellBI raiseEvent:event:params:] + 1008
 DoIP Test Client     -[B4IDelegatableResumableSub resume::] + 300
 DoIP Test Client     -[B4I checkAndRunWaitForEvent:event:params:] + 252
 DoIP Test Client     -[B4ICommon CallSubDebug4::::] + 248
 DoIP Test Client     -[B4ICommon CallSubDebug2::::] + 188
 DoIP Test Client     -[b4i_doiplink _processdoipmessage:::] + 2448
 DoIP Test Client     -[b4i_doiplink _m_tcpstreams_newdata::] + 1564
 CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 407796
 CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 407416
 DoIP Test Client     +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 776
 DoIP Test Client     -[B4IShell runMethod:] + 320
 DoIP Test Client     -[B4IShell raiseEventImpl:method:args::] + 932
 DoIP Test Client     -[B4IShellBI raiseEvent:event:params:] + 1008
 DoIP Test Client     __50-[B4I raiseEventFromDifferentThread:event:params:]_block_invoke + 24
 libdispatch.dylib    E1A24FA0-2160-3366-AE7E-420F0FA3C5BC + 6568
 libdispatch.dylib    E1A24FA0-2160-3366-AE7E-420F0FA3C5BC + 111076
 libdispatch.dylib    E1A24FA0-2160-3366-AE7E-420F0FA3C5BC + 232976
 libdispatch.dylib    E1A24FA0-2160-3366-AE7E-420F0FA3C5BC + 67940
 libdispatch.dylib    _dispatch_main_queue_callback_4CF + 44
 CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 663600
 CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 194052
 CoreFoundation       DDA24297-289C-30EC-8228-4C426AAD4D7F + 189772
 GraphicsServices     GSEventRunModal + 120
 UIKitCore            9EE8C19A-A370-3599-823B-34B84131AAD7 + 1184324
 UIKitCore            UIApplicationMain + 332
 DoIP Test Client     main + 96
 dyld                 02F772FD-9E38-35A4-B9FA-9CCE3A090E6D + 19484
)

if i put a breakpoint on the CallSub2 line and then step manually into it, it does not crash.

additional info: if i compile and run in Release mode, it runs fine with no crash at all.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
Does it work with CallSubDelayed? It is safer to call "Wait For events" with CallSubDelayed.
it works with CallSubDelayed

does this applies only for B4i Debug mode, or it's an all-around "code smell"?
because i expect CallSub to execute the called Sub immediately, while CallSubDelayed will call the Sub at the next "chance" (when the program return to the message loop?).
so it may require some rethinking of the program flow to be sure the use of the Delayed is fine.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend using CallSubDelayed for events in all platforms. With CallSub, the Wait For statement might be called after the event was fired and it will lead to a lost event. This will not happen with CallSubDelayed. And the pipeline of CallSubDelayed is much simpler than CallSub, which is especially relevant to debug mode.
 
Top