0

I'm trying to track down a memory leak and wanted to know how this works. In the BaseHelper class, there is a property for a sessionManager:

BaseHelper
@property(nonatomic, strong) SessionManager *sessionManager;

- (void)setup {
    __weak typeof(self) weakSelf = self;
    [self.sessionManager reachabilityChanged:^(BOOL isReachable) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf reachabilityChanged:isReachable];    
        });
    }]
}

So far, this part looks ok to me since it's using weakSelf to call reachabilityChanged.

- (void) reachabilityChanged:(BOOL)isReachabile {

    __block typeof(self) __weak weakSelf = self;

    [self fetchSettingsWithCompletion:^(BOOL success, NSError * _Nullable error) {

        if (success){

            [weakSelf processSettings];

        } else if (!success &&
                !error){

            [weakSelf errorHandling];

        }

    }];

}

Again, this part looks ok to me since it's using weakSelf.

- (void) processSettings {

    // New, saved, or default publish settings?

    [self setupLogger];

    [self setupOtherProperties];

}

Here if you don't use weakSelf and in other methods like setupLogger or setupOtherProperties if you don't reference weakSelf again, do you run the risk of capturing properties on the main object and having a retain cycle?

Crystal
  • 28,460
  • 62
  • 219
  • 393
  • Probably unrelated, don't use `__block` when using the `weakSelf` pattern. Only use `__block` when you need to be able to change the value of the variable, which is not the case here. – Rob Dec 26 '17 at 16:46
  • In answer to your question, not using `weakSelf` pattern inside `processSettings` is not the source of your problem. If you have a strong reference cycle, I'd suggest using the "debug memory graph" (introduced in Xcode 8), which will show you precisely what is causing the problem. Personally, I like using "malloc stack" option with "debug memory graph" feature as shown in https://stackoverflow.com/a/30993476/1271826. – Rob Dec 26 '17 at 16:50
  • You do not have problems with a retain cycle with blocks here. As the term *cycle* suggests, you can only have a cycle, if the block retains a object reference (`self` or another one, there is in theory nothing special with self) and the block itself is retained by the object. You do not have this. As suggested, use the tools to find the origin of the retain cycle. – Amin Negm-Awad Dec 26 '17 at 17:07

0 Answers0