Any way to apply weak() to a method object?

I want to have method object in a class but get errors about circular reference. So I tried to use the weak() modifier but couldn't get it to work. Is this not supposed to work? Am I doing something wrong?

The following code works just fine without the .weak() call but causes the circular reference error on exit. With .Weak() I get an error "Could not find Symbol invoke" when I try to call methodref.invoke().

class A
{
var methodref;
function initialize()
{
methodref = method(:doSomething).weak();
}

function callDoSomething()
{
methodref.invoke();
}

function doSomething()
{
}
}
  • In your example methodref would be an instance of Lang.WeakReference, which is an object type unto itself. You need to use methodref.get() to get a strong reference (or null). If that function returns a valid reference, you could call invoke() on that...

    class A
    {
    var methodref;

    function initialize() {
    methodref = method(:doSomething).weak();
    }

    function callDoSomething() {
    var strong = methodref.get();
    if (strong != null) {
    strong.invoke();
    }
    }

    function doSomething() {
    }
    }


    Another option is to not create the Lang.Method until you actually need it. For example...

    class A
    {
    var symbol;

    function initialize() {
    symbol = :doSomething;
    }

    function callDoSomething() {
    method(symbol).invoke();
    }

    function doSomething() {
    }
    }


    Travis
  • Thanks for the reply and explanation. I assumed I was just missing something. In my application, I actually have an array of these method objects and call them based on an index. The alternative is to use an if-then-else statement but like the index method object array better. Still trying to fully understand this language.
  • As another way to deal with this, is there some kind of dispose() method on objects that I could put code in to set these objects to null and free them?
  • Nope, there's nothing like that. The closest that you'll find are View.onHide() and App.onStop().

    Travis
  • Former Member
    Former Member over 7 years ago
    Hi there,

    I have a follow up question to this old thread. So sorry for hijacking this thread...

    I tried to implement your example from above. However, I have different classes I want to share some data so the structure looks a bit different.

    My original implementation, which works but returns a 'circular reference' warning once the program is ended looks simplified as follows:
    class model {

    protected var classA_Instance;

    function initialize() {
    self.classA_Instance = new classA(method(:notify));
    }

    function notify(type,value) {
    // do something
    }

    }

    // classA (in a different class file)
    class classA {

    protected var notify;

    function initialize(notifyObj) {
    self.notify = notifyObj;
    }

    function gotValue(type,value) {
    self.notify.invoke(type,value);
    }
    }


    when gotValue() is called it actually executes the notify function of class model. However, as mentioned, I get a circular reference warning.
    When I now apply your example regarding weak references I don't get a circular warning anymore but I never get a reference to the object with .get() and it returns always null:

    class model {

    protected var classA_Instance;

    function initialize() {
    self.classA_Instance = new classA(method(:notify).weak);
    }

    function notify(type,value) {
    // do something
    }

    }

    // classA (in a different class file)
    class classA {

    protected var notify;

    function initialize(notifyObj) {
    self.notify = notifyObj;
    }

    function gotValue(type,value) {
    var strongRef = self.notify.get();
    if (strongRef != null) {
    strongRef.invoke(type,value);
    }
    }
    }


    what am I missing?

    Thank you for your help in advance

    [EDIT]: I just found the solution in another thread: https://forums.garmin.com/forum/developers/connect-iq/137532-should-i-clean-my-circular-reference