gsklee / ngStorage

localStorage and sessionStorage done right for AngularJS.
MIT License
2.34k stars 461 forks source link

memory leak in ngstorage (while running jasmine) #245

Closed JanEggers closed 7 years ago

JanEggers commented 7 years ago

hi i have a close eye on memory and currently i have found a memory leak related to ngstorage.

ngstorage keeps my rootscopes alive while unittesting.

image

should not be relevant for live apps but is there a way to fix that?

nc-babelv commented 7 years ago

Are you using ngMock? We dealt with this issue in our tests by mocking $localStorage and $sessionStorage.

  module('ngStorage', ($provide) => {
    function mockStorage() {
      this.$apply = this.$default = this.$reset = this.$supported = this.$sync = () => {};
    }

    $provide.service('$sessionStorage', mockStorage);
    $provide.service('$localStorage', mockStorage);
  });

We're using jasmine and put that in a global beforeEach. Depending on your usage you may need to flesh the mocked functions out more.

saskodh commented 6 years ago

I also stumbled on this issue. These two event handlers should be unregistered on $rootScope.$on('$destroy').

$window.addEventListener('storage', function(event) {
    // ...
});

$window.addEventListener('beforeunload', function() {
    // ...
});