Xcode 4.6 ARC Warning for Game Center Authentication

后端 未结 2 609
失恋的感觉
失恋的感觉 2021-02-14 07:33

This is a new compiler warning that only showed up when I updated XCode to 4.6. My code is lifted directly from Apple\'s documentation (this is my iOS 6 code btw).

2条回答
  •  执笔经年
    2021-02-14 08:24

    The issue is that the localPlayer object is keeping a strong reference to itself - when localPlayer is "captured" for use within the authenticateHandler block, it is retained (when objective-c objects are referred to within a block, the compiler under ARC calls retain for you). Now, even when all other references to the localPlayer cease to exist, it will still have a retain count of 1 and hence the memory will never be freed. This is why the compiler is giving you a warning.

    Refer to it with a weak reference, like:

    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    
    __weak GKLocalPlayer *blockLocalPlayer = localPlayer;
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
        [self setLastError:error];
        if (blockLocalPlayer.authenticated) {
            ...
    

    Given that the lifetime of the authenticateHandler and the localPlayer are tightly linked (i.e. when the localPlayer is deallocated, so is the authenticateHandler) there's no need for it to maintain a strong reference within the authenticateHandler. Using Xcode 4.6, this no longer generates the warning you mentioned.

提交回复
热议问题