Iphone 已发布对象崩溃应用程序--alloc/init内存管理问题

Iphone 已发布对象崩溃应用程序--alloc/init内存管理问题,iphone,memory-leaks,memory-management,malloc,Iphone,Memory Leaks,Memory Management,Malloc,我分配并启动了一个flipView类。但是,当我发布它时,应用程序崩溃了。如果我不发布,这个应用程序就可以正常运行,看起来是这样。释放时收到的错误消息为: Malloc-对象错误:未分配要释放的对象指针 如果你能帮助我,我将不胜感激 - (IBAction)showInfo { FlippedProduceView *flippedView = [[FlippedProduceView alloc]initWithIndexPath:index]; flippedView.fl

我分配并启动了一个flipView类。但是,当我发布它时,应用程序崩溃了。如果我不发布,这个应用程序就可以正常运行,看起来是这样。释放时收到的错误消息为:

Malloc-对象错误:未分配要释放的对象指针

如果你能帮助我,我将不胜感激

- (IBAction)showInfo {
    FlippedProduceView *flippedView = [[FlippedProduceView alloc]initWithIndexPath:index];

    flippedView.flipDelegate = self;

    flippedView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentModalViewController:flippedView animated:YES];

    //[flippedView release]; //******** Maybe A Memory Leak *********\\
}

您的
presentModalViewController:
消息应在
flippedView
上调用
retain
。这将阻止它被释放,直到
presentModalViewController:
的目的完成。然后,您可以在本例程结束时调用
[flippedView release]
。除非缺少其他内容?

将最后一行放在那里是正确的,因为当您将“flippedView”作为“presentModalViewController”的参数传入时,它会在内部保留“flippedView”(无需编写任何其他代码)

苹果框架中的大多数函数都会保留一个对象,如果它们看起来在逻辑上应该保留的话。如果您正在呈现视图控制器,那么在任何情况下都不希望传递要呈现的已解除分配(或即将解除分配)的控制器。您在其中显示的包含视图控制器将保留子控制器,直到它被解除

因此,我们很清楚,下面是正确的代码(假设没有其他异常情况):


谢谢你回答我的问题。我是一个完全的新手,正在开发我的第一个应用程序。我几乎完成了,但这个问题使我停滞不前。我不明白我该如何编写你提到的表达式。是这样吗?:FlippedProduceView*fView=[[FlippedProduceView alloc]initWithIndexPath:index];[查看保留];fView.flipDelegate=self;fView.ModAltTransitionStyle=UIModAltTransitionStyleFlipHorizontal;[self-presentModalViewController:fView动画:是];NSLog(@“保留计数视图:%d”,[fView保留计数];[fView release];好吧,看起来像这样吗?[self-presentModalViewController:[fView retain]动画:是];在
presentModalViewController
内部,您将在收到的视图上调用
retain
。您不需要在
showInfo
中调用
retain
,因为alloc/init阶段会自动为您调用。然后,您可以从
showInfo
中调用
release
,该对象仍将存在,因为它被
presentModalViewController
保留。
- (IBAction)showInfo {

// Here the retain count gets incremented to 1 (usually "alloc" or "copy" does that)
FlippedProduceView *flippedView = [[FlippedProduceView alloc]initWithIndexPath:index];

// Retain count is unchanged
flippedView.flipDelegate = self;

// Retain count is unchanged
flippedView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

// Retain count is incremented again inside this method (to 2)
[self presentModalViewController:flippedView animated:YES];

// Retain count is decremented by 1 (back to 1)
[flippedView release]
}

// ... Other code

// Finally, whenever the view controller gets dismissed, it will be released again
// and the retain count will be 0, theoretically qualifying it for deallocation