How to safely transfer reference to object across window?

Posted by Morgan Cheng on Stack Overflow See other posts from Stack Overflow or by Morgan Cheng
Published on 2010-06-07T08:37:00Z Indexed on 2010/06/07 8:42 UTC
Read the original article Hit count: 266

I'm debugging a web application. Javasript in one window create one object and use it as argument to invoke global method in another window. Pseudo code is like below.

var obj = new Foo();
anotherWin.bar(obj);

In anotherWin, the argument is stored in global variable.

var g_obj;

function bar(obj)
{
   g_obj = obj;
   ...
}

When other function tries to reference g_obj.Id, it throws exception "Cannot evaluate expression". This happens in IE8.0.7600.16385 on Windows 7.

In Visual Studio debugger, when this exception happens, the g_obj shows as

 {...}

It looks all its properties are lost.

Perhaps the root reason is the object is created in one window but only referenced in another window. The object might be garbage-collected at any time.

Is there any way to work around this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about garbage-collection