You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have a hybrid app that uses React Native for a single view. When we navigate from it, there are some bytes that are not freed. On closer inspection, I noticed that some classes call [NSNotificationCenter addObserver:…] in init and [NSNotificationCenter removeObserver:…] in dealloc. With ARC enabled, before iOS 9.0, NSNotificationCenter will retain a strong reference so dealloc will never be called, causing a leak. See here for more details.
This accounts for some of the leaks we're currently seeing in our app but not all so we're still looking into it. Maybe this accounts for some of the leaks mentioned in #11961.
Solution
There are several solutions here:
Bump all deployment targets to iOS 9.0.
Only pass a weak reference to [NSNotificationCenter addObserver:…].
Move the call to [NSNotificationCenter removeObserver:…] out of dealloc to somewhere more appropriate, like viewWillDisappear: or equivalent.
I'm not sure which solution is more preferred here (hence the lack of a PR) but if you still want to support iOS 8.0, then I'd propose solution 2 as the simplest to implement:
With ARC enabled, before iOS 9.0, NSNotificationCenter will retain a strong reference
That's not true. Before iOS9, NSNotificationCenter holds an unsafe-unretained reference to your view, from iOS10 on it's a weak reference instead. Neither of these increase the retain count of your objects and don't prevent deallocation.
Passing a weak reference to NSNotificationCenter also doesn't work that way, since you're effectively turning it into a strong reference when using it as an argument.
Description
We have a hybrid app that uses React Native for a single view. When we navigate from it, there are some bytes that are not freed. On closer inspection, I noticed that some classes call
[NSNotificationCenter addObserver:…]
ininit
and[NSNotificationCenter removeObserver:…]
indealloc
. With ARC enabled, before iOS 9.0, NSNotificationCenter will retain a strong reference sodealloc
will never be called, causing a leak. See here for more details.This accounts for some of the leaks we're currently seeing in our app but not all so we're still looking into it. Maybe this accounts for some of the leaks mentioned in #11961.
Solution
There are several solutions here:
[NSNotificationCenter addObserver:…]
.[NSNotificationCenter removeObserver:…]
out ofdealloc
to somewhere more appropriate, likeviewWillDisappear:
or equivalent.I'm not sure which solution is more preferred here (hence the lack of a PR) but if you still want to support iOS 8.0, then I'd propose solution 2 as the simplest to implement:
Additional Information
The text was updated successfully, but these errors were encountered: