Status: New
Owner: ----

New issue 2028 by [email protected]: garbage collection issue
http://code.google.com/p/v8/issues/detail?id=2028

// Suggest you copy this page into a JS editor to read.
// @Part1, This part is the API implementation code:
require.define('apis', function (require, module, exports) {
    var _history = db.retriveObject(), //db is defined at other place
        _listener = {},
        _handler,
        _self;

    _self = {
        find: function (successCB, filter) {
var filterResults = _history; // key place! It will work well if I use utils.copy(_history);
            if (filter) {
                filterResults = filter(filterResults);
            }

            setTimeout(successCB(filterResults), 1);
        },
        addListener: function (observerObj) {
            _handler = Math.uuid(null, 16);

            _listener[handle] = observerObj;
        }
    };

    event.on("newItems", function (items) {
        var observerObj = _listener[_handler];

        _history.push(items);
        observerObj(items);
    });

    return _self;
});

// @Part2, The demo code:
var _historyList;

function successCB(results) {
    _historyList = results;
}

function addedCB(newItems) {
console.log(_historyList); // New items have been added into _historyList already here!
    _historyList = _historyList.concat(newItems);
}
apis.find(successCB, filter);
apis.addListener(addedCB);

// Problem description:
// In the addedCB function, when it's called for the fist time,
// the newItems will be in the _historyList before it's added! But after that, when the addedCB
// is called for the second time, no such thing happens.

// Some findings that related:
// After some debugging, I found that the "_historyList" variable in demo application has pointed // to the "_history" in apis module for the first time the addedCB is called. There is a connection
// between "_historyList" in demo and "_history" in the API module.

// The reason and problem that I guess:
// I guess that "filterResults" will be cleared automatically when the programme exit from "find" // function, but as it points to "_history", it won't be cleared immediately, it will be cleared
// automatically only when the "_history" changes.
// The problem is here: When the "_history" changes, all the local variables that point to // it should be cleared in the memory, but this should be done before "_history" changes, not after, // they may mistake the order so that before those variables are cleared, they got the new values and // those values are passed to any variable that points to it. For the second time it's called, the // connection has been broke so the "_historyList" in the demo won't get the latest value of "_history"
// in API module.

// As I am not familiar with the source code of V8, so the location of the problem has not been found in // the source code. Here I just report it and expect some expert to confirm it.


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to