jashkenas/backbone — pull request #3049 by smelnikov (2014-03-07 05:40:21+00:00)
Title: prevent memory leaks with stopListening and 'off

This PR fixes potential leaks when using listenTo and stopListening, it addresses the following:

```javascript
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);

b.on('y', fn);
a.listenTo(b, 'x', fn);
a.stopListening(b, 'x');
b = null; // b can't be garbage collected until a.stopListening() is called
```

```javascript
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
var c = _.extend({}, Backbone.Events);

a.listenTo(c, 'x', fn);
b.listenTo(c, 'x', fn);
c.off('x');
c = null; //c can't be garbage collected until 'stopListening()' is called on a and b
```

this implementation has a minimal impact on performance as it uses a counter to determine when a listener link can be safely broken.