user2727195
user2727195

Reputation: 7330

Does asynchronous code cause memory leaks?

I'm concerned if asynchronous operations are prone to memory leaks. My context is Javascript used both on the frontend as well as backend (node.js)

On the execute operation, a delegate IResponder is created. It has the reference to the main module, as well as success and fault methods.

The exampleModule performs the asyncOperation and calls the result or the fault method of the IResponder.

My question is: I hope exampleModule is deallocated at the end of the main routine as I see there are some circular references going on with closures etc. Please advise or confirm.

    execute: function() {
        var self = this;
        function IResponder() {
            this.result = function(data) {self.result(data)};
            this.fault = function(info) {self.fault(info)};
        }
        var exampleModule = new ExampleModule();
        exampleModule.asyncOperation(new IResponder());//calls back result or fault of IResponder
    },

    success: function(data) { //is exampleModule deallocated here?
    },
    fault: function(info) { // //is exampleModule deallocated here?
    }

Upvotes: 2

Views: 1108

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

Circular references do not cause problems for good JavaScript garbage collectors.

When your objects become eligible deallocation in this case depends on when ExampleModule lets go of its reference to your IResponder.

Upvotes: 4

Related Questions