B4J Question How correctly to destroy a class instance ?

peacemaker

Expert
Licensed User
Longtime User
HI, All

If a B4X class contains other B4X classes and the instances are created runtime - if they are views, usually they need to be:

  • created: init\create\show each instance of sub-classes
  • shown in an interface (and resized)
  • updated in an interface (and resized)
  • maybe moved by user
  • and destroyed
When an object is not needed for user - he should be able to delete it.
If object id is stored in a variable - nullification of variable does not help.
.IsInitialized of each created instance is always = True.


B4X:
Dim objs As List
...

Private Sub butRefresh_Click
    Editor.RemoveAllViews
    Init_Area
    For i = 0 To objs.Size - 1
        Dim obj As b4xVectEdit_component = objs.Get(i)
        obj = Null ' does not help
    Next
    Generate_Diagram
End Sub

Sub Generate_Diagram
    If b4xVectEdit_component1.IsInitialized = False Then
        objs.Add(b4xVectEdit_component1.Initialize(Me, "Unit1"))
        b4xVectEdit_component1.Name = "Unit1"
        b4xVectEdit_component1.DesignerCreateView(Editor, Null, Null)
    End If
    b4xVectEdit_component1.Base_Resize(500dip, 500dip)
  
    If b4xVectEdit_component2.IsInitialized = False Then
        objs.Add(b4xVectEdit_component2.Initialize(Me, "Unit2"))
        b4xVectEdit_component2.Name = "Unit2"
        b4xVectEdit_component2.DesignerCreateView(Editor, Null, Null)
    End If
    b4xVectEdit_component2.Base_Resize(100dip, 300dip)

    If b4xVectEdit_component3.IsInitialized = False Then
        objs.Add(b4xVectEdit_component3.Initialize(Me, "Unit3"))
        b4xVectEdit_component3.InternalView = True
        b4xVectEdit_component3.Name = "Unit3_internal"
        b4xVectEdit_component3.DesignerCreateView(b4xVectEdit_component1.mRect.mBase, Null, Null)
    End If
    b4xVectEdit_component3.Base_Resize(200dip, 220dip)
    b4xVectEdit_component3.mRoot.Left = 65dip
End Sub

How correctly in B4J to destroy such structure and free memory up for sure ? For re-creation again of a fresh set.
 
Last edited:
Solution
Not 100% sure what you are meaning, but If you have a class object that you need to reuse, just dim it again and it will be uninitialised. (just like when adding items to a table view - you create the item inside the loop)
B4X:
Dim a As Something
a.Initialize
log(a.isInitialized) ' True
'do something with a
...
' now I want to reuse a
Dim a As Something
Log(a.isInitialized) ' False

Daestrum

Expert
Licensed User
Longtime User
While a variable has a 'live' reference it will occupy memory and garbage collect will ignore it.

In your example code, the object has a reference stored in the list, so making another copy and setting to null will do nothing.

Original
B4X:
    For i = 0 To objs.Size - 1
        Dim obj As b4xVectEdit_component = objs.Get(i)
        obj = Null ' does not help
    Next

Modified
B4X:
    For i = objs.Size - 1 to 0 Step -1
        objs.RemoveAt(i) ' will remove the reference from list which in turn (assuming nothing else reference that object) will make the original object available to GC
    Next

It's a hard concept to understand, unike C where you have a destructor for objects/classes.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
objs.RemoveAt(i)

Anyway b4xVectEdit_component1.IsInitialized = always True.
Is it real to create some destructor here ? To kill the object right now and make sure it.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
In my code
B4X:
Sub Generate_Diagram
    If b4xVectEdit_component1.IsInitialized = False Then
        objs.Add(b4xVectEdit_component1.Initialize(Me, "Unit1"))
        b4xVectEdit_component1.Name = "Unit1"
        b4xVectEdit_component1.DesignerCreateView(Editor, Null, Null)
    End If
    b4xVectEdit_component1.Base_Resize(500dip, 500dip)

After first objects creation all components are with .IsInitialized = true.
Is it possible to make it again .IsInitialized = false ?

How to continue using the b4xVectEdit_component1 for another instance ?

upd.

Ahhh
B4X:
Sub Generate_Diagram
    Private b4xVectEdit_component1, b4xVectEdit_component2, b4xVectEdit_component3 As b4xVectEdit_component
    
    If b4xVectEdit_component1.IsInitialized = False Then
        objs.Add(b4xVectEdit_component1.Initialize(Me, "Unit1"))
        b4xVectEdit_component1.Name = "Unit1"
        b4xVectEdit_component1.DesignerCreateView(Editor, Null, Null)
    End If
    b4xVectEdit_component1.Base_Resize(500dip, 500dip)
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Not 100% sure what you are meaning, but If you have a class object that you need to reuse, just dim it again and it will be uninitialised. (just like when adding items to a table view - you create the item inside the loop)
B4X:
Dim a As Something
a.Initialize
log(a.isInitialized) ' True
'do something with a
...
' now I want to reuse a
Dim a As Something
Log(a.isInitialized) ' False
 
Upvote 0
Solution
Top