Complete
over 3 years ago

By design. 

weak() doesn't work or bug in simulator/ActiveMemory

this

code

class Cone
{
	var x;
	
	function initialize(parent)
	{
		x	= parent.weak().get();
	} 
}

class Ctwo
{
	var xs;
	
	function initialize()
	{
		xs = new[2];
		
		xs[0] = new Cone(self);
		xs[1] = new Cone(self);
	}
}

var t = new Ctwo();

in ActiveMemory I can see:

- t Obj #187 Circular Reference!
-- xs Obj #188
--- [0] Obj #189
----x Obj #187 Circular Reference!
...

  • I don't agree. If there is Circular Reference in

    x    = parent.weak().get();

    there will be too in

    exampleOfAccessingX

    weak() as Lang.WeakReference

    Get a WeakReference to an Object.

    A weak reference is an object that keeps a reference to an object but does not increment the reference count. This means the object reference can be destroyed, so is a case that should be handled.

    get() as Lang.Object or Null

    Get the Object referenced by the WeakReference

    Returns:

    • Lang.Object

      The Object referenced, or null if the Object no longer exists

    I don't know weak() return class WeakReference. Only for stillAlive? I can simple check null of object. Maybe just for not increment references it has to be special class.

  • I think you're misunderstanding how to use weak(). The point is to keep the weak reference around until you need it, not to call get() immediately. By calling get() immediately, you're storing a strong reference to the object, which defeats the purpose of using weak() in the first place.

    Try something like this instead:

    class Cone
    {
    //	var x;
        var xRef;
    
        function initialize(parent)
        {
            //x = parent.weak().get();
            xRef = parent.weak();
        }
    
        function exampleOfAccessingX() {
            var x = xRef.get();
            if (x != null) {
                // do something with x
            }
        }
    
    }
    
    class Ctwo
    {
        var xs;
    
        function initialize()
        {
            xs = new[2];
    
            xs[0] = new Cone(self);
            xs[1] = new Cone(self);
        }
    }
    
    var t = new Ctwo();

  • and where there is nowhere info about  Circular Reference

    Error Name: Circular Dependency Error
    Occurrences: 1
    First Occurrence: 2022-02-01
    Last Occurrence: 2022-02-01
    Devices:
        Venu® Sq: 3.20
    App Versions: 1.22.1
    Languages: ita
    Backtrace:
        PSX1_Vie.<init>:62

    sdk 4.0.9

    and nobody answer to my post Disappointed and now problems

    https://forums.garmin.com/developer/connect-iq/f/discussion/281058/circular-dependency-error-occurrences