【发布时间】:2011-06-02 10:41:35
【问题描述】:
我想知道在执行在 Objective C 中分配内存的命令后正确的方法是什么(我主要指的是 iOS 应用程序)。 我的困境来自这样一个事实,即检查内存分配操作是否成功会增加很多代码行,同时想知道这是否有用。 此外,有时内存分配是显而易见的,例如使用“alloc”,但有时它们是在幕后进行的。即使我们检查每一个分配——当我们发现它失败时——我们实际上也无能为力。所以也许正确的方法是让它失败并让应用程序崩溃? 看看这段代码:
// Explicit memory allocation
NSArray a1 = [[NSArray alloc] initWithObjects:someObj, nil];
if (!a1) {
// Should we make this check at all? Is there really what to do?
}
// Implicit memory allocation
NSArray a2 = [NSArray arrayWithObjects:someObj, nil];
if (!a2) {
// should we make this check at all? Is there really what to do?
}
您认为正确的方法是什么?检查或不检查分配失败?那里的 iOS 开发人员 - 你是如何在你的应用程序中处理它的?
【问题讨论】:
标签: iphone objective-c memory ios memory-management