gpt4 book ai didi

iphone - 释放对象,我理解正确吗?

转载 作者:行者123 更新时间:2023-11-28 20:46:00 25 4
gpt4 key购买 nike

我是 Object C 的新手,但我认为我的内存管理思想至少从根本上是正确的。我有这个简单的方法:

-(int)weekIdFromDate:(NSDate *)inDate {

NSCalendar *currentCal = [NSCalendar currentCalendar];
NSDateComponents *component = [currentCal components:NSWeekCalendarUnit fromDate:inDate];
int week = [component week];

//I don't understand why this fails, it seems correct to release these objects now that they
// are no longer needed
[currentCal release];
[component release];

return week;
}

该方法存在于我的 Root View Controller 中,但警告出现在“main”函数中。我用僵尸来定位“BAD_ACCESS ...”的原因。如果我将这些评论发布出来,一切正常,没有问题。

最佳答案

您正在释放不属于您的对象。

您用来获取currentCalcomponent 的方法返回autoreleased 对象,这不是您负责release.

您只能对使用new alloccopy 获取的对象或发送retain< 的对象调用释放 消息。 (记住助记符 NARC)。

全部在 Memory Management Guide 中解释

关于iphone - 释放对象,我理解正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6361234/

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