1

So say for example I have a factory:

app.factory('exposeService', function() {
    var service = {};
    service.expose = function(name, callback) {
        service[name] = callback;
    };
    service.destroyExposed = function(name) {
        if (service[name]) {
            service[name] = null;
        }
    };
    return service;
})

This factory is pretty simple it has two methods one to set a callback and the other to destroy said callback. A controller would consume this service and 'expose' its methods which other services can now call directly. Here's an example:

app.controller("simpleController", ["exposeService", "$scope", function(exposeService, $scope) {
    var vm = this;
    vm.counter = 0;
    var exposedCallback = function() {
       console.log('This is what I need to access: ', vm);
       vm.counter++;
    };

    playerOverlayService.expose('exposedCallback', exposedCallback);

    $scope.$on('$destroy', function() {
        playerOverlayService.destroyExposed('onSystemMessage');
    });
}]);

This controller needs to increment the counter whenever the service decides it's appropriate to call the callback. Now my question is, if I destroy the reference to the callback when the controller is destroyed, will the scope inside the callback leak memory because it is no longer being used or referenced? Or is the destroyExposed method good enough at telling the GC that it can clean up?

Pedro
  • 67
  • 8
  • You need to readup on this http://stackoverflow.com/a/27016855/2435473 – Pankaj Parkar Oct 15 '15 at 20:36
  • That's a great reference. However the topic only lightly touches on it. How would you go about weakening the references from inside the callback if the service isn't directly messing with the callback but just attaching it to itself for a short while. – Pedro Oct 15 '15 at 21:00

0 Answers0