iphone - 如何在for循环中释放一个对象?

标签 iphone objective-c ios ipad memory-management

我是 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”是一个潜在的泄漏。我试图在最后或使用 autorelease 释放它,但都没有正常工作。每次我调用我的应用程序终止的方法。再次感谢您!

最佳答案

在你的循环中,将每个 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];

编辑:在您的第三期中,您无法释放 View Controller ,因为它的 View 正在被窗口使用。将其设置为自动释放也会导致相同的命运,只是延迟了。

您必须在某处保留您的 GastroCategoriesView Controller ,例如在您的应用程序委托(delegate)的实例变量中。

关于iphone - 如何在for循环中释放一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5410254/

相关文章:

ios - 呈现 UIView 的最少代码

ios - 是否可以使用 xcode 时间分析器从您自己的库中查看跟踪?如果没有,是否有任何替代方案?

ios - 当我们点击 UIbutton 时,如何更改 TableList UILabel backGround Color?

iOS Metal Swift newComputePipelineStateWithFunction 不工作错误

ios - SceneKit 在不动时下降到 30FPS

ios - 如何将多级自定义对象保存到 UserDefaults?

iphone - 为什么图像没有显示在 UIImageView 中

iphone - 如何判断两个物体是否相交?

ios - 为什么iphone的视频是颠倒的?

ios - 检测在 UITableView 中按下了哪个 UIButton