TVOS TVML XMLHttpRequest memory leak crashes application

53 Views Asked by At

I have a simple TVML app that displays shop-floor status data, the JS hits a webservice every 60 seconds and pulls an updated TVML string and loads it to the DOM. It works great for about 4 hours then crashes due to a memory leak caused by XMLHttpRequest not being deposed of properly. I've tried various ways to release the object, but nothing seems to have any affect. Any suggestions?

Apple TV device log error is: 90 seconds cpu time over 90 seconds (100% cpu average), exceeding limit of 50% CPU over 180 seconds

App.onLaunch = function (options) {
    var ip = options.IP;
    var refreshRate = options.REFRESH_RATE;

    //reload js every 15 minutes
    setTimeout(function () { App.reload() }, 900000);

    //refresh at passed in interval
    setInterval(function () { getTemplate(ip) }, refreshRate);

    loadingMessage(ip);
    getTemplate(ip);
}

function pushDoc(doc) {
    if (doc != null) {
        navigationDocument.clear();
        navigationDocument.pushDocument(doc);
    }
}

function getTemplate(ip) {
    try {
        var url = "http://GetTemplate?ip=" + ip;
        var request = new XMLHttpRequest();

        request.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            pushDoc(request.responseXML);
            delete request;
            request = null;
            }
        };

        request.responseType = "document";
        request.open("GET", url);
        request.send();
    }
    catch (err) {
        errorMessage(ip, "getTemplate()", err.message)
    }
}

function loadingMessage(ip) {
    var loadingString = "<?xml version='1.0' encoding='UTF-8' ?>";
    loadingString += "<document>";
    loadingString += "<alertTemplate>";
    loadingString += "<title>IP: " + ip + " - " + now() + "</title>";
    loadingString += "<description>Loading...</description>";
    loadingString += "</alertTemplate>";
    loadingString += "</document>";

    var loading = new DOMParser();
    var doc = loading.parseFromString(loadingString, "application/xml");
    pushDoc(doc);
}

function errorMessage(ip, func, err) {
    var errorString = "<?xml version='1.0' encoding='UTF-8' ?>";
    errorString += "<document>";
    errorString += "<alertTemplate>";
    errorString += "<title>IP: " + ip + " - " + now() + "</title>";
    errorString += "<description>" + func + " failed! Error is: " + err + "</description>";
    errorString += "</alertTemplate>";
    errorString += "</document>";

    var error = new DOMParser();
    var doc = error.parseFromString(errorString, "application/xml");
    pushDoc(doc);
}

function now() {
    var currentdate = new Date();
    var datetime = ((currentdate.getMonth() + 1) < 10 ? '0' : '') + (currentdate.getMonth() + 1) + "/"
        + (currentdate.getDate() < 10 ? '0' : '') + currentdate.getDate() + "/"
        + currentdate.getFullYear() + " @ "
        + (currentdate.getHours() < 10 ? '0' : '') + currentdate.getHours() + ":"
        + (currentdate.getMinutes() < 10 ? '0' : '') + currentdate.getMinutes() + ":"
        + (currentdate.getSeconds() < 10 ? '0' : '') + currentdate.getSeconds();

    return datetime;
}
0

There are 0 best solutions below