造成奇怪崩溃的方法

时间:2010-02-18 18:44:48

标签: objective-c xcode methods crash-reports

2 个答案:

答案 0 :(得分:4)

这是错误的:

NSIndexPath *indexPath = [[NSIndexPath alloc] init];
indexPath = [self.categoryTableView indexPathForSelectedRow];

NSInteger selectedCategory = indexPath.row;
[indexPath release];

您正在实例化一个空的NSIndexPath,然后覆盖对它的引用。最后一行的发布将发送到表视图返回的完全不同的对象。您创建的对象永远不会被使用,也永远不会被释放。

这将有效:

NSIndexPath *indexPath = [self.categoryTableView indexPathForSelectedRow];
NSInteger selectedCategory = indexPath.row;

您没有创建NSIndexPath对象,因此您没有责任释放它。

(您的不必要的释放导致索引路径实例过早释放。当Apple的代码尝试释放该对象时,它不再存在,并且应用程序崩溃)

答案 1 :(得分:2)

这只是猜测,因为并非所有代码都存在。在Transaction类的init方法中,是否确保保留字符串? (类别和描述)。

对我来说最突出的是你初始化Transaction,然后立即释放categoryString。如果您没有保留字符串,那么这可能是您崩溃的原因。

另一方面,你正在泄漏记忆。这里:

NSString *descriptionString = [[NSString alloc] init];
descriptionString = descriptionField.text;

descriptionString指向一个新分配的字符串,只是被重新分配给现有字符串,因此第一个字符串被泄露。您应该将其更改为:

NSString *descriptionString;
descriptionString = descriptionField.text;

或更简单:

NSString *descriptionString = descriptionField.text;