How to safely transfer reference to object across window?
- by Morgan Cheng
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?