Hello all,

I'm facing a memory leak on my gadget only after the weekends :)
probably the code get too lazy to come back to work on Mondays ...
follow a code with only the network piece of the code that I used to
simulate the problem increasing the refresh time of the services.

thanks

[code]
function view_onOpen() {
        update1();
        update2();
        update3();
        update4();
}


var interval1 = 500;
var interval2 = 500;
var interval3 = 500;
var interval4 = 500;

function update1(){

        var reader1 = new newConnection( 'data1', 'http://127.0.0.1/data/
test1.php' );

        // only add the function if the connection is not active
        if( ! reader1.active ){
                reader1.getText( function(){
                        debug.trace('*** response 1');
                        label1.innerText = reader1.text;
                });
        }

/*
                // action #1 -- this way generates memory leak
                reader1.getText( function(){
                        debug.trace('*** response 1');
                        label1.innerText = reader1.text;
                });
*/
        view.setTimeout("update1()", interval1);
}


function update2(){

        var reader2 = new newConnection( 'data2', 'http://127.0.0.1/data/
test2.php' );


        // only add the function if the connection is not active
        if( ! reader2.active ){
                reader2.getText( function(){
                        debug.trace('*** response 2');
                        label2.innerText = reader2.text;
                });
        }

        view.setTimeout("update2()", interval2);
}

function update3(){

        var reader3 = new newConnection( 'data3', 'http://127.0.0.1/data/
test3.php' );


        // only add the function if the connection is not active
        if( ! reader3.active ){
                reader3.getText( function(){
                        debug.trace('*** response 3');
                        label3.innerText = reader3.text;
                });
        }

        view.setTimeout("update3()", interval3);
}

function update4(){

        var reader4 = new newConnection( 'data4', 'http://127.0.0.1/data/
loop.php' );


        // only add the function if the connection is not active
        if( ! reader4.active ){
                reader4.getText( function(){
                        debug.trace('*** response 4');
                        label4.innerText = reader4.text;
                });
        }

        view.setTimeout("update4()", interval4);
}


// original code

var connections = new Array();

// check if there is any connection that is trying more
// than the number of tries that is considered error
// when the connection suceed the tentatives # are reset
//
function isThereAnyError( group ){

        var foundError = false;

        // search for HttpRequest with the same id
        for (i=0;i<connections.length;i++) {

                debug.trace( '\t [' + connections[i].tentatives + '] '
                + connections[i].id + ': ' + connections[i].url
                );

                if( connections[i].tentatives >= LIMIT_TRIES
                        && connections[i].id.indexOf(group) == 0 ){
                        foundError = true;
                        break;
                }
        }

        var temp = '';
    // + ': ' + connections[i].url;

        // sort the list
    //connections = sortById(connections);

        for (i=0;i<connections.length;i++) {
                temp = temp + '\n[' + connections[i].tentatives + '] ' + 
connections
[i].id + ' ' + connections[i].url;
        }
        communication_debug_information = temp;
        return foundError;
}

// simple bubble sort
function sortById(items){

        var swapped = true;
        while( swapped ){
                swapped = false;

                for(var i=0;i<items.length-1;i++){
                        if( items[i].id > items[i+1].id){
                                var temp = input[i+1];
                                input[i+1] = input[i];
                                input[i] = temp;
                                swapped = true;
                        }
                }
        }

        return items;

}

// create a new connection, and search for a existing one
// for the same id, if exists check if is active, that means
// that the request is taking too long to respond then create
// a new request with the tentatives # increased
// when the request is not active the new request will reset the
// tentatives
//
function newConnection(id, url, custom){

        var found = false;
        var instance;

        // search for HttpRequest with the same id
        for (i=0;i<connections.length;i++) {

                if( connections[i].id == id ){
                        found = true;

                        // is is still active increase the tentatives until
                        // reaches the limit
                        if( connections[i].active ){
                                        debug.trace('active will +1 on 
tentatives');
                                        connections[i].tentatives = 
connections[i].tentatives +1;

                        } else {

                                // not active but with error
                                if( connections[i].error ){
                                        debug.trace('error +1 on retry and 
create a new one');
                                        var tentatives = 
connections[i].tentatives +1;
                                        connections[i].reset();
                                        connections[i] = null;
                                        connections[i] = new HttpRequest(id, 
url, custom );
                                        connections[i].tentatives = tentatives;

                                } else {
                                        debug.trace('not active and no error = 
reset the tries');
                                        connections[i].reset();
                                        connections[i] = null;
                                        connections[i] = new HttpRequest(id, 
url, custom );
                                        connections[i].tentatives = 0;
                                }

                        }
                        instance = connections[i];
                        break;
                }

                // if the ! connections[i].active then must remove from the list
        }

        // not found create a new
        if(!found){
                debug.error('not found will create a new one');
                instance = new HttpRequest(id, url, custom );
                connections[connections.length] = instance;
        }

        return instance;
}





// @see http://www.w3.org/TR/XMLHttpRequest/
//
function HttpRequest( id, url, custom ){

        this.id = id;
        this.url = url;
        this.custom = custom;
        this.request = null;
        this.status = null;
        this.text = null;
        this.stream = null;

        this.active = false;
        this.error = false;
        this.tentatives = 0;

        this.textCallBack = function(){ debug.info('No callback set')};

        this.get = doGet;
        this.onData = function(){ };
        this.onError = function(){ };

        this.reset = function (){
                if( this.request != null ){
                        this.request.abort();
                        this.request.onreadystatechange = null;
                }
                this.request = null;
        }

        this.getText = function ( textCallBack ){
                this.textCallBack = textCallBack;
                this.get( this.url, this.id );
        }

        // send the request and catch errors
        //
        function doGet( url, id ){
                // avoid multiple calls with the same object
                if( this.active ){
                        return;
                }

                this.active = true;
                var request = new XMLHttpRequest();
                request.onreadystatechange = processStateChangeCallBack;

                var owner = this;

                // Catch errors sending the request
                try {
                        request.open("GET", url, true);
                        debug.trace('SEND [' + id + '] ' + url );
                        request.send();

                } catch (e) {
                        this.status = 'error reading data ' + this.id;
                        debug.error( this.status );
                        this.active = false;
                        this.error = true;
                        this.onError();
                }

        // when the state of the HttpRequest changes
        //
        function processStateChangeCallBack(){

                if (request.readyState == 4 ){

                        if (request.status == 200) {
                                owner.stream = request.responseStream;
                                owner.text = request.responseText;
                                owner.onData(owner.url);

                                if( owner.textCallBack ){
                                        owner.textCallBack(owner.custom);
                                }

                                owner.reset();
                                request = null;
                                owner.active = false;

                        } else {
                                owner.status = "communication error : "
                                + request.status
                                + ", url="
                                + owner.url;

                                        debug.error(owner.status);
                                owner.onError(owner.url);
                                        owner.active = false;
                                        owner.error = true;
                                        request = null;
                        }
                }
        }

        }
}
[/code]
-- 
You received this message because you are subscribed to the Google Groups 
"Google Desktop Developer Group" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-desktop-developer?hl=en.


Reply via email to