B4R Question Unidentified misbehavior of code or B4R

hatzisn

Well-Known Member
Licensed User
Longtime User
Good afternoon everyone,

before almost 20 days or so I had post a problem I was having when I was trying to parse JSON. Here is the forum thread:

After @Daestrum advice I have changed slightly the code and now it returns a byte array and does not accept a ResultBuffer byte array in the arguments but it creates it in the sub and returns this instead as a sub result. With this modification I have managed to read different values of a JSON - at least that is what I though.

This is because after reading each new value it trashes slightly the previous read values. I am posting the code and the logs in order for you to have a visual first look of the problem but I also attach the project at the end. I run this code in a WeMos D1 Mini v3.0.0.

Module Main:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private bc As ByteConverter
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    Log("AppStart")
    'example of connecting to a local network
'    If wifi.Connect2(""SSID"", ""PASSWORD"") Then
'        Log(""Connected to network"")
'    Else
'        Log(""Failed to connect to network"")
'    End If


    GlobalStore.Put (3, "{""aa"":""23"",""id"":""1332278""}")
    GlobalStore.Put(4, "{""ssid"":""SSID_NAME"",""pass"":""SSID_PASS"",""lat"":""38.478711"",""long"":""22.663213"",""usr"":""hatzisn"",""usps"":""1234684658""}")

    Trial(0)
End Sub


Sub Trial(tag As Byte)
    Log("* * * We set blat from globalstore.slot4 * * *")
    Dim bLat() As Byte
    bLat = JSON.GetTextValueFromKey(GlobalStore.Slot4, "lat", Null, 0)
    Dim iBLAT As UInt = bLat.Length
    Log("iBLAT:" , iBLAT)
    Log("* * * We copy bLat to bCpLat and log the result of bCpLat to check if it destroys the original array only * * *")
    Dim bCpLat(iBLAT) As Byte
    bc.ArrayCopy(bLat, bCpLat)
    Log(bCpLat)

    Log(bLat.Length)
    Log("-", bLat, "-")
    Log("---")



    Log("* * * We set bLon from globalstore.slot4 * * *")
    Dim bLon() As Byte
    bLon = JSON.GetTextValueFromKey(GlobalStore.Slot4, "long", Null, 0)


    Log(bLon)
    Dim iBLON As UInt = bLon.Length
    Log("iBLON:", iBLON)

    Log("* * * We copy bLon to bCpLon and log the result of bCpLon to check if it destroys the original array only * * *")
    Dim bCpLon(iBLON) As Byte
    bc.ArrayCopy(bLon, bCpLon)
    Log(bCpLon)


    Log("* * * Now that we have created bCpLon we log bCpLat and see that it keeps last 4 digits correct and changes * * *")
    Log("* * * the first five digits to the last five of bCpLon * * *")
    Log("=================")
    Log(bCpLat)
    Log("=================")

    Log("----")


    Log("* * * We set bID from globalstore.slot3 * * *")
    Log("* * * We copy bID to bCpID and log the result of bCpID to check if it destroys the original array only * * *")

    Dim bID() As Byte
    bID = JSON.GetTextValueFromKey(GlobalStore.Slot3, "id", Null, 0)
    Log(bID)
    Log("------")
    Dim iBID As UInt = bID.Length
    Log("iBID:", iBID)
    Dim bCpID(iBID) As Byte
    bc.ArrayCopy(bID, bCpID)
    Log(bCpID)

    Log("* * * Now that we have created bCpID we log bCpLat and bCpLon and see that it has trashed completely bCpLat * * *")
    Log("* * * and in bCpLon has kept the .6 part and then* * *")
    Log("=================")
    Log(bCpLat)
    Log("=================")

    Log(" ")
    Log("=================")
    Log(bCpLon)
    Log("=================")

    Dim bLink() As Byte = "https://www.mysite.com/wthr/wthrcrnt.aspx?lat=".GetBytes
    Log(bLink)
    Dim iBLINK As UInt = bLink.Length
    Log("iBLINK:" , iBLINK)

    Dim bKaiLon() As Byte = "&lon=".GetBytes
    Log(bKaiLon)
    Dim iBKAILON As UInt = bKaiLon.Length
    Log("iBKAILON:", iBKAILON)


    Dim bKaiID() As Byte = "&id=".GetBytes
    Log(bKaiID)
    Dim iBKAIID As UInt = bKaiID.Length
    Log("iBKAIID:", iBKAIID)


    Log(StackBufferUsage)


    '*************************************************************************
    'SHOULD BE GIVING THE FOLLOWING LINE:
    '38.778711-22.763213-1332272
    '*************************************************************************
    'INSTEAD IT GIVES
    '13322781�-.613322-1332278
    '*************************************************************************
    Log(bCpLat, "-", bCpLon, "-", bCpID)



    Dim iLen As ULong = iBLINK + iBLAT + iBKAILON + iBLON + iBKAIID + iBID
    Log("iLEN: ", iLen)

    Dim bAll(iLen) As Byte
    Log("bAll.Length: ", bAll.Length)


    bc.ArrayCopy2(bLink, 0, bAll, 0, iBLINK)
    bc.ArrayCopy2(bCpLat, 0, bAll, iBLINK, iBLAT)
    bc.ArrayCopy2(bKaiLon, 0, bAll, iBLINK + iBLAT, iBKAILON)
    bc.ArrayCopy2(bCpLon, 0, bAll, iBLINK + iBLAT + iBKAILON, iBLON)
    bc.ArrayCopy2(bKaiID, 0, bAll, iBLINK + iBLAT + iBKAILON + iBLON, iBKAIID)
    bc.ArrayCopy2(bCpID, 0, bAll, iBLINK + iBLAT + iBKAILON + iBLON + iBKAIID, iBID)

    '*************************************************************************
    'SHOULD BE GIVING:
    'https://www.mysite.com/wthr/wthrcrnt.aspx?lat=38.778711&lon=22.763213&id=1332278
    '*************************************************************************
    'INSTEAD IT GIVES:
    'https://www.mysite.com/wthr/wthrcrnt.aspx?lat=13322781�&lon=.613322&id=1332278
    '*************************************************************************
    Log(bAll)


    '*************************************************************************
    'THIS LINE IN THE ORGINAL PROJECT BREAKS BUT HERE IT GIVES THE SAME RESULTS:
    'https://www.mysite.com/wthr/wthrcrnt.aspx?lat=13322781�&lon=.613322&id=1332278
    '*************************************************************************
'    Dim bAll() As Byte = JoinBytes(Array(bLink, bCpLat, bKaiLon, bCpLon, bKaiID, bCpID))
'    Log(bAll)



End Sub


And here is what this code prints in logs:
********************* PROGRAM STARTING ****************
�����l,�:�tO��4��`�AppStart
* * * We set blat from globalstore.slot4 * * *
iBLAT:9
* * * We copy bLat to bCpLat and log the result of bCpLat to check if it destroys the original array only * * *
38.478711
9
-38.478711-
---
* * * We set bLon from globalstore.slot4 * * *
22.663213
iBLON:9
* * * We copy bLon to bCpLon and log the result of bCpLon to check if it destroys the original array only * * *
22.663213
* * * Now that we have created bCpLon we log bCpLat and see that it keeps last 4 digits correct and changes * * *
* * * the first five digits to the last five of bCpLon * * *
=================
632138711
=================
----
* * * We set bID from globalstore.slot3 * * *
* * * We copy bID to bCpID and log the result of bCpID to check if it destroys the original array only * * *
1332278
------
iBID:7
1332278
* * * Now that we have created bCpID we log bCpLat and bCpLon and see that it has trashed completely bCpLat * * *
* * * and in bCpLon has kept the .6 part and then it has added the first five digits of the bID * * *
=================
13322781�
=================

=================
.613322
=================
iBLINK:46
&lon=
iBKAILON:5
&id=
iBKAIID:4
12648
13322781�-.613322-1332278
iLEN: 80
bAll.Length: 80


I do not have the slightest clue on what is going wrong here. If someone of you can see the mistake please go on and share it with me. That is if there is a mistake in the code and it is not a minor bug of B4R so please @Erel have a look too now that it is public.
 

Attachments

  • PossibleBug.zip
    4.5 KB · Views: 124
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is a mistake to change the way the json subs work: https://www.b4x.com/android/forum/threads/json-parsing.107410/
Returning arrays of bytes from a sub is less efficient and is more likely to be handled incorrectly as the same stack memory is reused.

Why do you need to change it?

This is the output that I get with your code, tested on ESP8266:

* * * We set blat from globalstore.slot4 * * *
iBLAT:9
* * * We copy bLat to bCpLat and log the result of bCpLat to check if it destroys the original array only * * *
38.478711
9
-38.478711-
---
* * * We set bLon from globalstore.slot4 * * *
22.663213
iBLON:9
* * * We copy bLon to bCpLon and log the result of bCpLon to check if it destroys the original array only * * *
22.663213
* * * Now that we have created bCpLon we log bCpLat and see that it keeps last 4 digits correct and changes * * *
* * * the first five digits to the last five of bCpLon * * *
=================
632138711
=================
----
* * * We set bID from globalstore.slot3 * * *
* * * We copy bID to bCpID and log the result of bCpID to check if it destroys the original array only * * *
1332278
------
iBID:7
1332278
* * * Now that we have created bCpID we log bCpLat and bCpLon and see that it has trashed completely bCpLat * * *
* * * and in bCpLon has kept the .6 part and then* * *
=================
13322781
=================

=================
.613322
=================
iBLINK:46
&lon=
iBKAILON:5
&id=
iBKAIID:4
12648
13322781
-.613322-1332278
iLEN: 80
bAll.Length: 80
&lon=.613322&id=1332278
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Nice. It seems that the wrong version of JSON functions were stored in my computer when I decided to use them. I changed the code to the link you have suggested but still I am not able to get the correct results.

Can somebody check with the following test project?
 

Attachments

  • PossibleBug.zip
    3.1 KB · Views: 121
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is what I get when I run your code:
B4X:
AppStart
{"aa":"23","id":"1332278"}
{"ssid":"SSID_NAME","pass":"SSID_PASS","lat":"38.478711","long":"22.663213","usr":"hatzisn","usps":"1234684658"}
* * * We set blat from globalstore.slot4 * * *
-38.478711-
---
-22.663213-
-----
-1332278-
------
https://www.mysite.com/wthr/wthrcrnt.aspx?lat=
&lon=
&id=
72
322782131-332278213-1332278
https://www.mysite.com/wthr/wthrcrnt.aspx?lat=322782131&lon=332278213&id=1332278
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
It seems that my project will crawl for Eternity. I have managed to locate the error which was at the version of code that was stored in my disk but an other error occured. I will open a new thread for the new error but let me first explain what was wrong so others can benefit too. In the code that I already uploaded in my previous post (corrected) everything is correct but the declaration of the byte arrays that will receive the new values. They are not set explicitly. F.e.

B4X:
Dim bLat() As Byte

'Should be declared explicitly:

'f.e.
Dim bLat(30) As Byte

Then everything works fine (until further notice).
 
Upvote 0
Top