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

Global variables won't hold a weakref?
Mephisto
#1 Posted : Tuesday, February 16, 2016 6:09:34 AM(UTC)
Rank: Member

Groups: Registered
Joined: 12/5/2010(UTC)
Posts: 20
Location: Roslindale, MA

Thanks: 9 times
Was thanked: 2 time(s) in 2 post(s)
I have a global variable that always seems to hold an instance rather than the weakref that it was assigned:

Code:

g_wrTest <- null;

class myClass
{
    constructor(id)
    {
        print ("Creating object " + id + "\n");
    }
}

function myFunc()
{
    local thing1 = myClass(1);
    local thing2 = myClass(2);

    local wrTest = thing1.weakref();
    g_wrTest = thing1.weakref();
    print ("g_wrTest = " + g_wrTest + "\n");
    print ("wrTest = " + wrTest + "\n");

    local wrTest = thing1;
    g_wrTest = thing1;
    print ("g_wrTest = " + g_wrTest + "\n");
    print ("wrTest = " + wrTest + "\n");

    local wrTest = thing2.weakref();
    g_wrTest = thing2.weakref();
    print ("g_wrTest = " + g_wrTest + "\n");
    print ("wrTest = " + wrTest + "\n");
}

myFunc();



I would expect to see:
g_wrTest = (weakref: 0x0352AC38)
wrTest = (weakref : 0x004C6790)
g_wrTest = (instance : 0x0352AC38)
wrTest = (instance : 0x0352AC38)
g_wrTest = (weakref: 0x0352AC98)
wrTest = (weakref : 0x034748D8)

Instead, I see this:
g_wrTest = (instance: 0x0352AC38)
wrTest = (weakref : 0x004C6790)
g_wrTest = (instance : 0x0352AC38)
wrTest = (instance : 0x0352AC38)
g_wrTest = (instance: 0x0352AC98)
wrTest = (weakref : 0x034748D8)

It's like the global variable refuses to hold a weakref. Note that the local wrTest variable displays results as expected.
fagiano
#2 Posted : Tuesday, February 16, 2016 1:41:17 PM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 1,059

Thanks: 0 times
Was thanked: 79 time(s) in 61 post(s)
From the documentation:

When a weak references is assigned to a container (table slot,array,class or instance) is treated differently than other objects; When a container slot that hold a weak reference is fetched, it always returns the value pointed by the weak reference instead of the weak reference object. This allow the programmer to ignore the fact that the value handled is weak.( see http://www.squirrel-lang...ge/weak_references.html )

g_wrTest does hold a weak reference, but when you fetch it the "roottable" will always return the actual object rather than the weak reference. This is by design.

ciao
Alberto
Follow me on Twitter @squirrellang
1 user thanked fagiano for this useful post.
Mephisto on 2/17/2016(UTC)
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.092 seconds.