Skip to content

PushNotificationIOS loses reference to listeners when adding multiple #21618

Closed
@carbonjesse

Description

@carbonjesse

Environment

React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Memory: 46.90 MB / 16.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 10.8.0 - ~/.nvm/versions/node/v10.8.0/bin/node
Yarn: 1.9.4 - ~/.yarn/bin/yarn
npm: 6.2.0 - ~/.nvm/versions/node/v10.8.0/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
Android SDK:
Build Tools: 23.0.1, 24.0.2, 25.0.0, 25.0.2, 25.0.3, 26.0.0, 26.0.2
API Levels: 23, 24, 25, 26, 27
IDEs:
Android Studio: 3.1 AI-173.4907809
Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
npmPackages:
@storybook/react: ^3.4.0 => 3.4.10
@storybook/react-native: ^3.4.0 => 3.4.10
@types/react: ^16.0.0 => 16.4.13
react: 16.6.0-alpha.8af6728 => 16.6.0-alpha.8af6728
react-native: 0.57.2 => 0.57.2
npmGlobalPackages:
react-native-cli: 2.0.1

Description

Calling

PushNotificationIOS.addEventListener("notification", this.myCallback);
PushNotificationIOS.addEventListener("notification", this.myCallback2);

PushNotificationIOS.removeEventListener("notification", this.myCallback);
PushNotificationIOS.removeEventListener("notification", this.myCallback2);

causes two listeners to be created via PushNotificationEmitter.addListeners.
However since they are tracked in PushNotificationIOS.js in the _notifHandlers map, the second call overwrites the first.

So the first removeEventListener fails to remove the listener. It is an orphaned reference and can cause memory leaks.

https://github.com/facebook/react-native/blob/master/Libraries/PushNotificationIOS/PushNotificationIOS.js#L247

I believe I can fix this if it is indeed a bug/improvement that should be fixed.

Reproducible Demo

Calling addEventListener twice, followed by removeEventListener twice should show this error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Platform: iOSiOS applications.Ran CommandsOne of our bots successfully processed a command.Resolution: LockedThis issue was locked by the bot.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions