Bug? [SOLVED] Process_Global List Variable and Private Sub List Variable Conflict Puzzle

Mashiane

Expert
Licensed User
Longtime User
Hi

Which other loopholes should I be aware of in terms of variable declaration?

I have declared a Process_Global variable,

B4X:
Private fContents As List

In my Private Sub GetFileContents() I have also declared
B4X:
Dim fContents As List

My multiple GetFileContents() calls only run once. In my private Sub GetFileContents, after I replaced fContents as List with fContent as List, my code worked perfectly.

Thing is, I have defined code like this in relation to strings and other variables but the List type is conflicting.
 

Mashiane

Expert
Licensed User
Longtime User
Hi Erel

Please find attached the NOT working version of the app.

B4X:
' get the type of file the code file is
private Sub GetCodeFileType(sPath As String) As String
    If File.Exists("", sPath) = False Then Return "Unknown"
    Dim fContents As List
    Dim fFirstLine As String
    Dim fType As String
    ' read the file contents
    fContents = File.ReadList("", sPath)
    ' read the first line of the file
    fFirstLine = fContents.Get(0)
    fType = jMash.MvField(fFirstLine,2,"=")
    Return fType
End Sub

As indicated on top, when I change the variable here from fContents to fContent, the app works perfectly. I have assumed perhaps that because I have a global variable defined as Private fContents As List, its the cause of the conflict. The libraries used in the app are in the Libraries folder inside the zip. Maybe there is something I'm missing somewhere.

With the wrong version, the sub routine runs once and stops thus the tree is not loaded. The corrected version works perfectly with the variable name change.

Thanks.
 

Attachments

  • jMashProjectProfile.b4j.zip
    81.6 KB · Views: 231

Mashiane

Expert
Licensed User
Longtime User
This is a large project. It is difficult for me to go over it.

In B4X local variables never hide global variables. This means that the following code:
B4X:
Sub MySub
Dim SomeGlobalList As List
SomeGlobalList.Initialize
End Sub

Will cause the global SomeGlobalList to be initialized.
Perhaps its a memory leak somewhere within my code or in libraries I'm using or there is something I'm really missing. A small project testing this issue yields no errors at all. I have combed the code and checked each instance of the variable fContents used and checked if this value gets assigned anything outside the main OpenProject subroutine and there is nothing. I decided to Log it where fContents gets changed or read..

B4X:
Waiting for debugger to connect...
Define global fContents As List variable...
Program started.
Assign global fContents to File.ReadList...
Read global fContents size inside OpenProject...
520
Read global fContents size inside OpenProject...
520
Read global fContents size inside OpenProject...
520
Define fContents inside GetCodeFileType sub routine...
Assign fContents to File.ReadList inside GetCodeFileType...
4537
Read fContents size inside GetCodeFileType...
Read global fContents size inside OpenProject...
4537
Read global fContents size inside OpenProject...
4537

For some reason, my global fContents list variable gets replaced with the fContents private variable. The size increases from 520 items to 4537 items and I cant find the problem, perhaps I'm tired. Anyway, just replacing the variable name from fContents to fContent in the GetCodeFileType subroutine makes the app work perfectly.
 
Top