简体   繁体   中英

Why does this snippet cause a leak?

I have a class method:

+ (UIBarButtonItem*)navItemWhichAddsNewAction{
    UIBarButtonItem* addActionButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_add"] style:UIBarButtonItemStylePlain target:nil action:nil];
    addActionButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal empty];
    }];
    return addActionButton;
}

Then, in –viewDidLoad , I assign the result of this method to the right button of the navigationItem :

self.navigationItem.rightBarButtonItem = [MyClass navItemWhichAddsNewAction];

If I then profile the app with Instruments , the rac_command block above is always reported as allocated even when the view controller (and the navigation controller) is deallocated.

Why?

Signal doesn't complete, and leak may be caused by this. Another side-effect may be that action is not called second time because of this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM