framework7io / framework7

Full featured HTML framework for building iOS & Android apps
http://framework7.io
MIT License
18.13k stars 3.23k forks source link

Memory leak in the once()/off(). Event not unbound #3001

Closed slenz closed 5 years ago

slenz commented 5 years ago

Describe the bug

Memory leak happens when event bound with once() and then unbound with off(). Event handler will not unbound.

To Reproduce

Run the kitchen-sink and watch for the memory allocations in the Chrome devtools. When open and close the "Photobrowser" component several times you will see multiple objects which have pointers to 'swipeBackMove'.

Expected behavior

Event handler unbound and removed

Actual Behavior

I seen this in the framework7-3.6.6/packages/core/js/framework7.js (line 6792)

    function onModalLoaded() {
      // Create Modal
      var modal = app[modalType].create(modalParams);
      modalRoute.modalInstance = modal;

      var hasEl = modal.el;

      function closeOnSwipeBack() {
        modal.close();
      }
      modal.on((modalOrPanel + "Open"), function () {
        if (!hasEl) {
          // Remove theme elements
          router.removeThemeElements(modal.el);

          // Emit events
          modal.$el.trigger(((modalType.toLowerCase()) + ":init " + (modalType.toLowerCase()) + ":mounted"), r
          router.emit(((!isPanel ? 'modalInit' : '') + " " + modalType + "Init " + modalType + "Mounted"), mod
        }
        router.once('swipeBackMove', closeOnSwipeBack);
      });
      modal.on((modalOrPanel + "Close"), function () {
        router.off('swipeBackMove', closeOnSwipeBack);
        if (!modal.closeByRouter) {
          router.back();
        }
      });

Event bound with router.once('swipeBackMove', closeOnSwipeBack) but failed to unbind in router.off('swipeBackMove', closeOnSwipeBack);

It happens because .once() create a wrapper function which bound with .on() and when router.off('swipeBackMove', closeOnSwipeBack); searches for closeOnSwipeBack() it does not found it and not unbound.

Screenshoot

devtools - proj local- f7-framework7-3 6 6-kitchen-sink-core-index html_351

nolimits4web commented 5 years ago

Fixed in latest 3.x and 4.x, should be better now