gpt4 book ai didi

iphone - 释放的对象使应用程序崩溃——alloc/init 内存管理问题

转载 作者:行者123 更新时间:2023-12-03 21:10:17 26 4
gpt4 key购买 nike

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

Malloc - error for object: object pointer being freed was not allocated.

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

- (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 *********\\
}

最佳答案

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

Apple 框架中的大多数函数都会保留一个对象(如果逻辑上看起来应该如此)。如果您正在呈现一个 View Controller ,那么实际上在任何情况下您都不想传递要呈现的已释放(或即将被释放)的 Controller 。您在其中呈现的包含 View Controller 将保留子 Controller ,直到它被关闭为止。

所以我们很清楚,这是正确的代码(假设没有其他异常情况):

- (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

关于iphone - 释放的对象使应用程序崩溃——alloc/init 内存管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3711728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com