【问题标题】:crashed on iphone but not on simulator在iphone上崩溃但在模拟器上没有
【发布时间】:2010-12-19 01:17:27
【问题描述】:

发现 iphone 和模拟器之间的许多差异真的令人难以置信。我花了几个小时试图弄清楚为什么我的应用程序在模拟器上运行但在我的 iPhone 设备上崩溃了。原来罪魁祸首是 sortedArrayUsingDescriptors。还有更多这样的你吗?请与我分享。

与您分享问题和修复:


代码在 iphone 上崩溃但不是模拟器

NSSortDescriptor* aDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease];
NSArray* anNsArray = [[NSArray alloc] init];
NSArray* aSortedNsArray = [[NSArray alloc] init];

aSortedNsArray = [anNsArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:aDescriptor]];

问题在[NSArray arrayWithObject:aDescriptor];


修复方法是创建一个数组来存储它:

NSArray* descriptorArray = [[NSArray alloc] initWithObjects:countDescrp, nil];
aSortedNsArray = [anNsArray sortedArrayUsingDescriptors:descriptorArray];

韦恩在加利福尼亚州坎贝尔

【问题讨论】:

    标签: iphone crash ios-simulator


    【解决方案1】:
    NSSortDescriptor* aDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease]; 
    NSArray* anNsArray = [[NSArray alloc] init];
    NSArray* aSortedNsArray = [[NSArray alloc] init];
    
    aSortedNsArray = [anNsArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:aDescriptor]];
    

    这是一个错误的初始化机制,如果代码 sn-p 是完整的,那么你的问题就出在空的 anNsArray 对象上。

    您也不需要初始化 aSortedNsArray。

    应该是这样的:

    NSSortDescriptor* sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease]; 
    
    // Assume you return it as array of objects from a property or method
    NSArray *array = [self objects]; 
    NSArray *sortedArray = nil;
    if ([array count] > 0) {
         sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    }
    
    // Then you check the sortedArray
    if (sortedArray == nil || [sortedArray count] == 0)
       [self somethingIsWrong];     
    

    arrayWithObject: (autoreleased) 或 initWithObject: (manual) 只是分配 NSArray 对象的不同方式。它不会导致正常崩溃。因为你关心的是 sortedArray 不保留描述符数组对象。

    【讨论】:

      【解决方案2】:

      尼古拉,

      你可能是对的。大多数编码人员都很难排除使用 Objective C 进行不良内存管理的可能性。如果我发现了您所推测的另一个真正的潜在错误,我将在这里更新。同时,我会提醒编码人员注意arraywithobjects 和initwithobjejcts 之间的区别;明智地使用它们。感谢您的回复。

      韦恩

      【讨论】:

      • arrayWithObject:initWithObject: 之间的区别是显而易见且众所周知的。这就是我向您推荐 Cocoa 内存管理指南的原因:它都在其中,应该被视为每个 Cocoa 程序员的基础。
      【解决方案3】:

      尼古拉,

      当我的应用程序简单而小时,它并没有崩溃。这可能与本文所述的自动释放和释放有关:http://kosmaczewski.net/2009/01/28/10-iphone-memory-management-tips/ 作者指出了另一个但类似的问题:

      “我敢肯定,您在使用 NSDictionary 的 dictionaryWithObjects:forKeys: 时遇到过应用程序崩溃的情况,然后发现用 initWithObjects:forKeys: 替换它让您的应用程序运行良好。”

      使用 [NSArray arrayWithObject:aDescriptor],使用自动释放创建 NSArray;相反,使用 [[NSArray alloc] initWithObjects:countDescrp, nil] 需要明确何时释放 NSArray。

      代码的简单更改使我的应用程序在 iphone 上没有 100% 的时间崩溃,而旧代码使应用程序在 100% 的时间崩溃。

      【讨论】:

      • 再次:您发布的代码不对您遇到的崩溃负责。在您的问题中,您说sortedArrayUsingDescriptors 在设备上无法按预期工作,这是错误的。您的错误可能是由于内存管理不善造成的。只是四处寻找,在不了解发生了什么的情况下将arrayWithObject: 切换为initWithObject: 并没有真正的帮助。我的建议是:阅读并理解 Cocoa 内存管理指南:developer.apple.com/mac/library/documentation/cocoa/Conceptual/…
      【解决方案4】:

      发布的代码不会崩溃。虽然有几个泄漏的对象,但它不会在模拟器上崩溃,也不会在设备上崩溃。

      我认为,您的问题出在其他地方。尝试使用新项目缩小范围并仅复制可疑代码。

      【讨论】:

        猜你喜欢
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 1970-01-01
        • 2012-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多