Bug? Crash changes place between compilations, but line is always 44

Sandman

Expert
Licensed User
Longtime User
When I compile my project and then run it I fairly instantly get the following crash:
B4X:
Application_Start
STARTING APP: startApp ================== 02/24/2022
Error occurred on line: 44 (B4XPagesManager)
*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]
Stack Trace: (
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 597328
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 1616192
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 1606500
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 129420
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 298888
  MyNiceAppVers10b     -[ResumableSub_cl_net_status resume::] + 760
  MyNiceAppVers10b     -[b4i_cl_net _status:] + 336
  MyNiceAppVers10b     -[ResumableSub_cl_serverstate_timerState_Tick resume::] + 1040
  MyNiceAppVers10b     -[b4i_cl_serverstate _timerstate_tick:] + 336
 MyNiceAppVers10b     -[b4i_cl_serverstate _initialize::] + 800
 MyNiceAppVers10b     -[b4i_b4xmainpage _initialize::] + 1856
 MyNiceAppVers10b     -[b4i_b4xpagesmanager _initialize:::] + 1164
 CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 133488
 CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 247368
 MyNiceAppVers10b     +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1300
 MyNiceAppVers10b     -[B4IShell runMethod:] + 448
 MyNiceAppVers10b     -[B4IShell raiseEventImpl:method:args::] + 2172
 MyNiceAppVers10b     -[B4IShellBI raiseEvent:event:params:] + 1580
 MyNiceAppVers10b     __33-[B4I raiseUIEvent:event:params:]_block_invoke + 52
 libdispatch.dylib    95D85335-DD46-355E-905C-3743A10000EC + 405924
 libdispatch.dylib    95D85335-DD46-355E-905C-3743A10000EC + 410024
 libdispatch.dylib    95D85335-DD46-355E-905C-3743A10000EC + 283832
 CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 317576
 CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 45448
 CoreFoundation       CFRunLoopRunSpecific + 572
 GraphicsServices     GSEventRunModal + 160
 UIKitCore            263D772F-F5F7-3C3A-A622-9B78CC000F53 + 5159824
 UIKitCore            UIApplicationMain + 332
 MyNiceAppVers10b     main + 120
 dyld                 start + 444
)
Application_Active

I bet I have some error, but that's not the reason for this thread. The thing is that between compilations the error "moves around", according to the crash log. Sometimes it's in one of my classes, sometimes it's in Main, and sometimes it's in B4XPagesManager (as seen above). I can manipulate this a bit by changing the timing. For instance by adding a breakpoint somewhere, or inserting a small Sleep(1000) somewhere in Main. The only thing that is static is that the error is in line 44 - every single time. And of course those lines look very innocent, with code such as "End Sub" and the like.

I've cleaned the project several times, I use latest version of B4i and I compile with the hosted builder.

So I'm thinking this might point to some sort of bug in the compiler/debugger/etc of B4i. I've compressed the full project directory (not exported the projected, because I imagine for this it's better to get the full thing), and have saved it for Erel if he wants to take a look at it.
 

Sandman

Expert
Licensed User
Longtime User
The debugger uses two different execution pipelines, depending on the code state and the existence of breakpoints. This is probably the cause of this difference.
Would this also explain why there's a difference with no breakpoints but a Sleep?

Can you post the code in cl_net_status?
I don't have a cl_net_status. But do have a class CL_NET and in it a sub status, so I'm assuming that's what you're asking for:
B4X:
Sub status As ResumableSub

'    LogColor("CLNET: status", Colors.Green)

    Dim url As String = mothershipUrl & "status"

    Dim values As Map = CreateMap( _
        "token": B4XPages.MainPage.CLKVS.Get("token") _
    )
    
    Dim files As List = Null
    
    Wait For (generic_request(url, values, files)) complete (result_map As Map)

    Dim info_map As Map = result_map.Get("info")
    
    If 200 = info_map.Get("status_code") Then
        failedStatusCount = 0
    Else
        failedStatusCount = failedStatusCount + 1
    End If

    Return Null

End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Would this also explain why there's a difference with no breakpoints but a Sleep?
Good question. Resumable subs are completely different than non-resumable subs. The compiler turns your sub code into a state machine.

I think that the error is related to you returning Null from generic_request (or not returning anything).
 

Sandman

Expert
Licensed User
Longtime User
I think that the error is related to you returning Null from generic_request (or not returning anything).
But I haven't touched that code for years, and it's battle tested on thousands of devices out there. Plus it compiled just fine a couple of days ago.

I've changed code in other places of my app (relating to geofences, if that matters), but nothing close to any net-related code.

So I can't even imagine why the crash log mentions it...
 

Sandman

Expert
Licensed User
Longtime User
Sure:
B4X:
Private Sub generic_request (endpoint As String, values As Map, files As List) As ResumableSub

    Dim CLAPI As CL_API
    CLAPI.Initialize(Me, headers)
    CLAPI.Communicate(endpoint, values, files)
   
    Wait For (CLAPI) Finished (result_map As Map)

    processStatus(result_map.Get("status"))

    Return result_map

End Sub
 

Sandman

Expert
Licensed User
Longtime User
Updated generic_request:
B4X:
Private Sub generic_request (endpoint As String, values As Map, files As List) As ResumableSub

    Log("Entering generic_request")

    Dim CLAPI As CL_API
    CLAPI.Initialize(Me, headers)
    CLAPI.Communicate(endpoint, values, files)
    
    Wait For (CLAPI) Finished (result_map As Map)

    Log("map: " & result_map)

    processStatus(result_map.Get("status"))

    Return result_map

End Sub

And this is the crash log:
B4X:
Application_Start
STARTING APP: startApp ================== 02/24/2022
token
Error occurred on line: 45 (B4XPagesManager)
*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]
Stack Trace: (
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 597328
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 1616192
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 1606500
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 129420
  CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 298888
  MyNiceAppVers10b     -[ResumableSub_cl_net_status resume::] + 760
  MyNiceAppVers10b     -[b4i_cl_net _status:] + 336
  MyNiceAppVers10b     -[ResumableSub_cl_serverstate_timerState_Tick resume::] + 1040
  MyNiceAppVers10b     -[b4i_cl_serverstate _timerstate_tick:] + 336
 MyNiceAppVers10b     -[b4i_cl_serverstate _initialize::] + 800
 MyNiceAppVers10b     -[b4i_b4xmainpage _initialize::] + 1856
 MyNiceAppVers10b     -[b4i_b4xpagesmanager _initialize:::] + 1164
 CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 133488
 CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 247368
 MyNiceAppVers10b     +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1300
 MyNiceAppVers10b     -[B4IShell runMethod:] + 448
 MyNiceAppVers10b     -[B4IShell raiseEventImpl:method:args::] + 2172
 MyNiceAppVers10b     -[B4IShellBI raiseEvent:event:params:] + 1580
 MyNiceAppVers10b     __33-[B4I raiseUIEvent:event:params:]_block_invoke + 52
 libdispatch.dylib    95D85335-DD46-355E-905C-3743A10000EC + 405924
 libdispatch.dylib    95D85335-DD46-355E-905C-3743A10000EC + 410024
 libdispatch.dylib    95D85335-DD46-355E-905C-3743A10000EC + 283832
 CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 317576
 CoreFoundation       31E5CA68-6CA4-30D5-AD16-BB231341E9BD + 45448
 CoreFoundation       CFRunLoopRunSpecific + 572
 GraphicsServices     GSEventRunModal + 160
 UIKitCore            263D772F-F5F7-3C3A-A622-9B78CC000F53 + 5159824
 UIKitCore            UIApplicationMain + 332
 MyNiceAppVers10b     main + 120
 dyld                 start + 444
)
Application_Active

As you can see, not even the initial Log("Entering generic_request") is in the crash log.

All of the netcode is very cross platform, and just to be sure I tried just now to install exact same on Android - works perfectly.


I decided to check if the sub status was even entered, so I uncommented line 3 in the code in post #3. Ran it, and worked like a charm.

To me this seems crazy, how can this be possible? It's like there's no relation between cause and effect.

For completeness, here's the log now that it randomly worked:
B4X:
Application_Start
STARTING APP: startApp ================== 02/24/2022
CLNET: status
token
Entering generic_request
INFO: MTRANS HAD ALL TERMS IN ALL LANGUAGES
*** mainpage: B4XPage_Created
Application_Active
*** mainpage: B4XPage_Appear [mainpage]
*** mainpage: B4XPage_Resize [mainpage]
isRegistered
*** pgregisterstart: B4XPage_Created [mainpage]
*** mainpage: B4XPage_Disappear [pgregisterstart]
*** pgregisterstart: B4XPage_Appear [pgregisterstart]
*** pgregisterstart: B4XPage_Resize [pgregisterstart]
map: NSMapTable {
[7] info -> NSMapTable {
[1] time -> 283
[6] status_code -> 401
[12] error_message ->
}
[10] siteslist -> NSMapTable {
}
[12] status -> NSMapTable {
}
[13] response -> NSMapTable {
}
}
Class (b4i_cl_api) instance released.
(There's some random things in there, that's just debug log messages from the KVS.)

By the way, this is repeatable. If I comment that line 3, it crashes. If I uncomment it, it works. I've tried several times in a row, it's a solid 100% match here.
 

Sandman

Expert
Licensed User
Longtime User
By the way, this is repeatable. If I comment that line 3, it crashes. If I uncomment it, it works. I've tried several times in a row, it's a solid 100% match here.

Small update: If I clean the project, the above is still true - but the crash log now says the crash happened in CL_KVS instead.
 

Sandman

Expert
Licensed User
Longtime User
The value is null and you cannot put null values in B4i maps.
I didn't know that, so I searched the forum to check what I've missed. Seems people are putting null in B4i maps now and then, so that's probably causing some problems for them. I also found you posting in 2017 that we shouldn't use nulls in B4i maps. Seems I've missed (or forgotten) that post. I suggest adding an entry for this in your Code Smells post.

Thank you very much for taking a look, and finding my code error. Looking forward to more helpful error messages from the IDE. :)
 
Top