Premature Death of a Process Global Variable

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I have a weird condition that has been hard to track down. Whenever it would fail in Release it would give the function name and say line 596 which didn't fit because those lines are like 70-100 and it would just say that a variable needed to be initialized first. This was strange since in that function I just load some values out of a database to a local List that is initialized there and uses a local Cursor that is setup using a Process Global SQL variable in a Code Module that is initialized as soon as the app starts and used in various Activities throughout.

I've been running it in Debug trying to track it down and it failes on this line:

B4X:
dbCursor = Utility.DB.ExecQuery("SELECT CaseNum FROM Cases ORDER BY CaseNum")

It is saying my DB (SQL) variable isn't initialized. On one device it seems to happen every once in a while when I switch to another app somehow or press Home then return to my app which then resumes right where I left off and reloads the Case list. On another device it is actually my personal phone and I got a phone call...when I hung up and it returned to my app it crashed on the same line. It appears that even though the process is running and those variables are being used that Android is deleting them from memory.

Now, I call other activities and come back to this one all the time and it loads my cases without issue. So, how can I prevent Android from deleting a Process Global when the process is still running??? Adding isInitialized checks and re-initializing it everywhere kind of defeats the purpose of making a Process Global...
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I've found two threads of users with similar issues:

http://www.b4x.com/forum/basic4andr.../9196-regarding-process_global-variables.html

http://www.b4x.com/forum/basic4andr...int-process-variables-loose-their-values.html

The solution Erel suggests on the first doesn't really make sense as that is exactly what I'm doing is initializing the SQL variable in Main's Create with:

B4X:
Utility.CreateDB
If Utility.db.IsInitialized = False Then
   ToastMessageShow("Unable to Open Database", True)
   Activity.Finish
End If

The 2nd thread suggested testing FirstTime in each Activity it is used in which could possibly work, but seems like more overhead. And, that is assuming it is set since it really isn't the first time otherwise it wouldn't be resuming where I left off. I already call this everytime Main is returned to, the screen is rotated in Main, etc. Main is just the Logon screen though, this Process Global is used everywhere. It again defeats the purpose of a Process Global and makes it really an Activity Global. If Process Globals don't stay active with the Process why have them???
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
So you are saying your process is destroyed , but on resume you skip the main screen?
This can definitely happen, don't expect process globals to retain their values. If there is so much inter dependence you can write them to file. However this should be a rare occurrence.

Sent from my GT-I9000 using Tapatalk 2
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Process global variables will hold the value as long as the process runs.
On one device it seems to happen every once in a while when I switch to another app somehow or press Home then return to my app which then resumes right where I left off and reloads the Case list
The process is probably killed when your app goes to the background.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I'm not sure how else to explain it since I don't clear them and at least 2 others reported it too. I think I will convert it to use all locals and Store my Globals in a Map wrote out to file and Read with each Activity needing it. A little more overhead than I wanted, but I don't want to be tracking down bugs for a year because of the OS garbage collection working overtime and cleaning things it shouldn't.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I added code at the beginning of every Create to make sure the SQL variable is always available. I left the other simple variables like Int and String that are in that same code module alone and have messed with it for a few hours now and not only have I not got the error, but the Int and String variables don't appear to be reseting. Perhaps it is a bug/issue with the SQL Type since it is accessing a File and more complex that something in it is getting garbage collected and causing the SQL variable to reset? I have a little more faith in Process Globals now...until I see it dropping my Int and String values.
 

Penko

Active Member
Licensed User
Longtime User
I was having a similar issue with my SQL object being reset. What helped me is to change the way I am doing things. I've just put lazy initialization to all my SQL-related logic. You check if the object is alive and initialize it on-demand, not in the beginning. That should happen before every query.

Sent from my HTC Desire using Tapatalk
 
Top