PDA

View Full Version : "Can't execute code from a freed script"


sakthivelcs
12-31-2009, 12:14 PM
I have a set of Iframe created dynamicaly in a page. I have some JavaScript objects defined in main page as well as in the pages that is loaded in the iframes. I need to call function from main page to pages in iframe and vice versa. I want to use event- subscriber model .i.e. objects in the iframe will subscribe to the application events from the main page.

For this i have

In Main page

var mainModule = {};

mainModule.EventList =[];

mainPage.subscribeEvent = function(object, funName)
{
var eventObject = {};
eventObject.EventObj = evntObject;
eventObject.EventHandler = funName
mainPage.EventList.push(eventObject);
}



in the page that is loaded in the iframe i have

subModule = {};
subModule.prototype.init = function()
{
top.mainPage. subscribeEvent( this, this.appEventHandler );
}

subModule.prototype.appEventHandler = function()
{
// handler code.
}


When an application event occurs in the main page, i iterate through the array and call the subscriber's function by

var eventObject = eventArray[icount];
if (!eventObject.EventObj) return;
eventObject.EventHandler.apply(eventObject.EventObj, argArray);

This code generally works. The issues is when the iframe is destroyed, the reference to the subscriber's object and the function exists in the eventlist.So when application event occurs, I get the error "Can't execute code from a freed script" . Is there any method in javascript to check whether the object is valid.

Gjslick
01-05-2010, 01:32 AM
Hmm, you could try a few things.

1) Before an iframe's page leaves (or the iframe is destroyed), unsubscribe any subscribed functions. You can possibly do this with a window.onunload event handler from the iframe's page. The iframe's page itself would just have to keep track of the functions that it subscribed.

2) Possibly test for the existence of the subscribed function itself before calling it from your 'notify' code. I don't know if internet explorer (which seems to be the only browser that will complain about executing code in a "freed" script) still keeps a reference to the function (although it most likely does, as it informs you that you're trying to execute "freed" code), but you could try something like this:

var eventObject = eventArray[ icount ];
if( !eventObject.EventObj ) return;
if( eventObject.EventHandler && typeof( eventObject.EventHandler ) == 'function' )
eventObject.EventHandler.apply( eventObject.EventObj, argArray );
3) Worst comes to worst, you can try executing the function in a try/catch from your 'notify' code. This might not be the best method though, as you won't be informed of any legitimate JavaScript errors that occur in your subscriber callbacks (unless you work with the exception object provided, possibly with its 'message' property).


var eventObject = eventArray[ icount ];
if( !eventObject.EventObj ) return;
try {
eventObject.EventHandler.apply( eventObject.EventObj, argArray );
} catch( exception ) {
// Uncomment to check error message.
// Can possibly build an if statement from it too
// alert( exception.message );
}


Let me know if that helps.