YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Weakref problem
Bimbol
#1 Posted : Wednesday, May 12, 2021 11:17:27 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/18/2015(UTC)
Posts: 5

Thanks: 2 times
Was thanked: 0 time(s) in 0 post(s)
Hello,

I've got problem with weak reference. Event after destroying object, I can still access it through weakref.

Sample code:
Code:

local function test() {
   
}

class Signals {
    elements = null

    constructor() {
        this.elements = []
    }

    function connect(callback, context) {
        this.elements.push([callback, context])
    }
}

class A {
    signals = null

    constructor() {
        this.signals = Signals()
    }
}

class B {
    a = null

    constructor() {
        this.a = A()
        this.a.signals.connect(test, this)
    }
}

local b = B()
local c = b.weakref()

b = null
print(c.ref())

I guess expected value returned by ref method should be null.
absence
#2 Posted : Sunday, May 23, 2021 6:42:52 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 141
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
This is because the class instance is not deleted, but only the variable b is.
You built a circular reference inside your class instances.

B creates and stores (references) A instance, which creates and stores Signal instance, which got B as a parameter and in turn stores and references it.
Hence B referenecs A, A references Signal and Signal references B inside its member elements.
As long as B instance is referenced ANYWHERE (even within itself), it's weakref will not return null.

To resolve this, you need to call garbagecollector after b=null (if you have compiled GC in at all).
However, circular references are a weak spot of Squirrel and hence a bad habit to create.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.053 seconds.