n3o*_*3on 7 iphone memory-management objective-c ipad ios
我是cocoa/objective-c的新手,而且我正在使用我的对象的版本.我有以下代码:
gastroCategoryList = [[NSMutableArray alloc] init];
for (NSDictionary *gastrocategory in gastrocategories) {
    NSString *oid = [gastrocategory objectForKey:@"id"];
    GastroCategory *gc = [[GastroCategory alloc] initWithId:[oid intValue] name:[gastrocategory objectForKey:@"name"]];
    [gastroCategoryList addObject:gc];
}
分析仪告诉我,for中定义的"gastrocategory"是潜在的内存泄漏.但我不确定我是否可以在for循环结束时释放它?
同样在以下代码中:
- (NSArray *)eventsForStage:(int)stageId {
    NSMutableArray *result = [[NSMutableArray alloc] init];
    for (Event *e in eventList) {
        if ([e stageId] == stageId) {
            [result addObject:e];
        }
    }
    return result;
}
分析仪告诉我,我的"结果"是潜在的泄漏.但是我应该在哪里发布这个?
当我应该在@property使用分配,复制,保留等时,是否还有一个简单的规则来记忆?
另一个问题:
- (IBAction)showHungryView:(id)sender {
    GastroCategoriesView *gastroCategoriesView = [[GastroCategoriesView alloc] initWithNibName:@"GastroCategoriesView" bundle:nil];
    [gastroCategoriesView setDataManager:dataManager];
    UIView *currentView = [self view];
    UIView *window = [currentView superview];
    UIView *gastroView = [gastroCategoriesView view];
    [window addSubview:gastroView];
    CGRect pageFrame = currentView.frame;
    CGFloat pageWidth = pageFrame.size.width;
    gastroView.frame = CGRectOffset(pageFrame,pageWidth,0);
    [UIView beginAnimations:nil context:NULL];
    currentView.frame = CGRectOffset(pageFrame,-pageWidth,0);
    gastroView.frame = pageFrame;
    [UIView commitAnimations];
    //[gastroCategoriesView release];
}
我不明白,"gastroCategoriesView"是一个潜在的泄漏.我试图在最后释放它或使用自动释放但是都不能正常工作.每次我调用方法我的应用程序都会终止.再一次非常感谢你!
在循环中,将它们gc添加到列表后释放每个,因为您不再需要它在循环范围中:
gastroCategoryList = [[NSMutableArray alloc] init];
for (NSDictionary *gastrocategory in gastrocategories) {
    NSString *oid = [gastrocategory objectForKey:@"id"];
    GastroCategory *gc = [[GastroCategory alloc] initWithId:[oid intValue] name:[gastrocategory objectForKey:@"name"]];
    [gastroCategoryList addObject:gc];
    [gc release];
}
在您的方法中,声明result要自动释放以从您的方法中解除对它的所有权:
NSMutableArray *result = [[[NSMutableArray alloc] init] autorelease];
// An alternative to the above, produces an empty autoreleased array
NSMutableArray *result = [NSMutableArray array];
编辑:在第三期中,您无法释放视图控制器,因为窗口正在使用它的视图.将其设置为自动释放也会导致相同的命运,只会延迟.
您必须将GastroCategoriesView控制器保留在某处,例如在应用程序委托的实例变量中.
| 归档时间: | 
 | 
| 查看次数: | 704 次 | 
| 最近记录: |