Is there a reason why you can't obtain a weak reference of an object inside the onUpdate function of a view?

Is there a reason why you can't obtain a weak reference of an object inside the onUpdate function of a view?

class controller {
    var test = "it works";
}

class TestView extends WatchUi.View {

    var controller;
    
    function initialize(controller) { // Passed in as a weak reference here
        self.controller = controller;
        var itWorksHere = controller.get()
        System.println(itWorksHere.test); // print "it works"
        
    }

    function onUpdate(dc) {
        // HERE
        var con = controller.get();
        System.println(con.test); // Causes the error below
        
    }
}

Here is the crash I get

Error: Unexpected Type Error
Details: Failed invoking <symbol>
Stack: 
  - onUpdate() at [Path] 0x100122bf 
Encountered an app crash

  • Does anyone have a strong reference to controller (i.e. the code that passed it in)?

    Otherwise, it seems that controller could've been destroyed.

    What if you call controller.stillAlive() in onUpdate()? What does it return (true or false)?

    And what if you just print the value of con as opposed to con.test? (If controller.stillAlive() returns false, I would expect con to be null).