次のコードでは、xCode の Build & Analyze 関数が
165 行目に割り当てられ、「addButton」に格納されたオブジェクトの潜在的なリーク。
addButton は、自動解放されたオブジェクトを返すカテゴリ barItemWithImage (ここで読んだもの) を使用する UIBarButtonItem です。addButtonItem を保持しないと、解放されたオブジェクトにアクセスしようとすると例外が発生します。
ここで何が欠けていますか?
        UIBarButtonItem *addButton;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        addButton = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"RedPlus.png"] target:self action:@selector(createStoryModal:)];
    }else {
        addButton = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"RedPlusiPhone.png"] target:self action:@selector(createStoryModal:)];
    }
    [addButton retain];
    NSArray* toolbarItems = [NSArray arrayWithObjects:
                             addButton,
                             [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                           target:nil
                                                                           action:nil],
                             nil];
    [toolbarItems makeObjectsPerformSelector:@selector(release)];
    self.toolbarItems = toolbarItems;
カテゴリ コード:
    @implementation UIBarButtonItem(MyCategory)
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}
@終わり