Variables/Scope question

terryn

Member
Licensed User
Longtime User
Hi,
I have been using B4A for a few months now and there is just one thing that has been puzzling me.
If a non-primative variable which has been dimed as local inside a sub is referenced from a global variable, why does it still hold the reference when the sub has ended.

B4X:
Sub NewGauge(X As Int,y As Int,w As Int,h As Int)
Dim Newp As Panel
Dim Newc As Canvas
Dim Newgb As GaugeBase ' Custom type
Newgb.Initialize
Newp.Initialize("Gauge")
Activity.AddView(Newp,X,y,w,h)
Newc.Initialize(Newp)
Newgb.cBase = Newc
Newgb.pBase = Newp
Newgb.pBase.Tag = Id
BaseList.Add(Newgb)
End Sub

I would have expected BaseList.cBase to lose the reference when Newc goes out of scope, or am I being a complete idiot.
Please dont shout at me, I have a headache.

Terry
 

terryn

Member
Licensed User
Longtime User
Sorry Erel, I didnt explain myself very well.
BaseList is a List of custom types (GaugeBase). One of the elements in GaugeBase is a Canvas. These are both global to the application.

B4X:
Sub Globals
Type GaugeBase(pBase As Panel, cBase As Canvas,cBmp As Canvas)
Dim BaseList As List
...

In the NewGauge Sub, I Dim and Initialise a new GaugeBase and add it to BaseList.

B4X:
Sub NewGauge(X As Int,y As Int,w As Int,h As Int)
Dim Newp As Panel
Dim Newc As Canvas
Dim Newgb As GaugeBase ' Custom type
Newgb.Initialize
BaseList.Add(Newgb)
End Sub

BaseList now contains a reference to Newgb which was dimed in the Sub.
What I dont understand is, why that reference is not broken when the Sub ends.
In all the other pass-by-reference languages I have used, if you pass a reference to a variable created in a subroutine and then try to access it after the sub has ended, you would get a runtime error.

I am not saying that I think this is wrong, I just want to know how this mechanism works. Does the locally created object/variable stay in memory?

Terry
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
Reference Counting

Hi Terry,

in Java (and similar languages) an object is only released when it's reference counter is 0.
But before leaving your sub the reference counter of the Canvas object is 2, as it's referenced by two objects, Newc and BaseList.cBase.

By leaving your sub the reference from NewC is removed, but the reference is still not 0. Therefore the Canvas object still exists.
Only when BaseList.cBase is nulled (e. g. by leaving the variable scope) the reference counter falls finally to zero and sooner or later - as Erel already mentioned - the garbage collector will remove it from the memory/heap.
 
Upvote 0

terryn

Member
Licensed User
Longtime User
Thank you very much for your replies. This has been bugging me for a while now and I'm sure I am not the only person wondering about this.

I think I will have to be very carefull to release those references when not needed.

Thanks again both of you, I am now a happy man.

Terry
 
Upvote 0

gkc1956

Member
Licensed User
Longtime User
Hi There,

I am confused as well, especially because in the tutorial Android Process and activity life it says :

Local variables are local to the containing sub. Once the sub ends these variables no longer exist.
Global variables can be accessed from all subs.


Gordon
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Local variables are local to the containing sub. Once the sub ends these variables no longer exist.
Global variables can be accessed from all subs.

What did you dont understand?

B4X:
Sub Globals
  dim myString as string
end sub
B4X:
Sub somesub
  dim myvariable as string
  ' myvariable is only valid inside this sub
end sub
Sub mysub
  log(myvariable) ' will ot work cause the variavle is only valid inside somesub
end sub
sub anothersub
  log(myString) ' will work cause myString is declared as global
end sub
 
Upvote 0
Top