【问题标题】:Method causing a weird crash导致奇怪崩溃的方法
【发布时间】:2011-01-18 11:22:17
【问题描述】:

我正在构建这个应用程序,但遇到了一些我以前从未见过的问题。

  1. 当我运行下面连接到UIButton 以触发触发器的方法时,应用程序崩溃。因此,当我按下按钮时,它会打印,然后模拟器会撞到跳板。如果我从跳板再次启动应用程序,它不会进入打印方法,如果我从 Xcode 重建它会这样做,然后像以前一样立即崩溃。

  2. 通常调试控制台中有一些信息,我至少可以从中了解正在发生的事情,但那里似乎没有太多信息。我注意到的一件事是,当应用程序最初在模拟器中启动时,打开了可访问性或某些东西,我不记得它是否总是存在:

2010-02-18 10:35:19.024 现金[497:207] ******* 辅助功能状态已更改:2010-02-18 10:35:19.134 现金[497:207] ********** 加载 AX 对于:com.yourcompany.Cash ************

版权所有 2010 Nicholas Iannone:

-(IBAction) logTransaction {

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

NSInteger selectedCategory = indexPath.row;
[indexPath release];
NSString *categoryString = [[NSString alloc] init];
categoryString = [self.categoryArray objectAtIndex:selectedCategory];
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 1000.0f;
[locationManager startUpdatingLocation];
double x = 10.0;
double y = 10.0;
NSString *descriptionString = [[NSString alloc] init];

descriptionString = descriptionField.text;
//[locationManager stopUpdatingLocation];

Transaction *newTransaction = [[Transaction alloc] initWithAmount:    [self.amountField.text integerValue] 
                                                      description: descriptionString 
                                                         category: categoryString   
                                                        locationX: x 
                                                        locationY: y];

                                      [categoryString release];
[newTransaction print];

 //log transaction here...

[newTransaction release];

这是控制台输出:

2/18/10 10:35:19 AM Cash[497] ********** 加载 AX 对于: com.yourcompany.Cash ************ 2/18/10 10:35:28 AM Cash[497] 0.000000,sddssd,零食,2010-02-18 10:35:28 -0800,10.000000,10.000000 2/18/10 10:35:28 AM com.apple.launchd.peruser.501[91] (UIKitApplication:com.yourcompany.Cash[0x9148][497]) 作业似乎已经崩溃: 分段错误 2/18/10 10:35:28 AM SpringBoard[482] 应用程序“现金” 信号 11 异常退出: 分段错误 2/18/10 10:35:28 AM ReportCrash[491] 保存的崩溃报告 对于现金[497] 版本??? (???) 到 /Users/knyck2/Library/Logs/DiagnosticReports/Cash_2010-02-18-103528_Nicholas-Iannones-MacBook-Pro.crash

这是崩溃日志:

流程:现金 [497] 路径:
/用户/knyck2/库/应用程序 支持/iPhone 模拟器/3.2/Applications/F54FA00C-9E77-4B9A-8774-2D5B5488A1BA/Cash.app/Cash 标识符:现金版本:
??? (???) 代码类型:X86 (原生)父进程:launchd [91]

日期/时间:2010-02-18 10:35:28.631 -0800 操作系统版本:
Mac OS X 10.6.2 (10C540) 报告 版本:6

异常类型:EXC_BAD_ACCESS (SIGSEGV) 异常代码: KERN_INVALID_ADDRESS 在 0x0000000014600478 线程崩溃:0 调度队列:com.apple.main-thread

应用特定信息: objc_msgSend() 选择器名称:release iPhone 模拟器 3.2 (193),iPhone 操作系统 3.2 (7B298g)

线程 0 崩溃:调度队列: com.apple.main-thread 0
libobjc.A.dylib
0x9805aedb objc_msgSend + 27 1
UIKit
0x002b4a19 _UIApplicationHandleEvent + 8468 2 图形服务 0x02f1f2a8 PurpleEventCallback + 1576 3 核心基础
0x026bb87e CFRunLoopRunSpecific + 4094 4 核心基础
0x026ba878 CFRunLoopRunInMode + 88 5 图形服务
0x02f1da4d GSEventRunModal + 217 6
图形服务
0x02f1db12 GSEventRun + 115 7 UIKit 0x002b265d UIApplicationMain + 1134 8 现金
0x00002648 主 + 102 (main.m:13) 9
现金
0x000025b6 开始 + 54

线程 1:0 libSystem.B.dylib
0x937b88da mach_msg_trap + 10 1
libSystem.B.dylib
0x937b9047 mach_msg + 68 2
核心基金会
0x026bb022 CFRunLoopRunSpecific + 1954 3 核心基金会
0x026ba878 CFRunLoopRunInMode + 88 4 网络核心
0x03151933 RunWebThread(void*) + 467 5 libSystem.B.dylib
0x937e5fbd _pthread_start + 345 6
libSystem.B.dylib
0x937e5e42 thread_start + 34

线程 0 因 X86 线程状态而崩溃 (32 位): eax:0x07802a50 ebx: 0x00017711 ecx:0x9655d0c4 edx: 0x07833ba2 edi:0x14600478 esi: 0x00000000 ebp: 0xbfffe158 特别是: 0xbfffdfd4 ss:0x0000001f efl: 0x00010206 eip: 0x9805aedb cs: 0x00000017 ds: 0x0000001f es: 0x0000001f fs: 0x00000000 gs: 0x00000037 cr2: 0x14600478

二进制图像: 0x1000 - 0x5ff7 +现金??? (???) /用户/knyck2/库/应用程序 支持/iPhone 模拟器/3.2/Applications/F54FA00C-9E77-4B9A-8774-2D5B5488A1BA/Cash.app/Cash 0xd000 - 0xfff7 +PBGDBIntrospectionSupport.A.dylib ??? (???) /Developer/Applications/Xcode.app/Contents/PlugIns/GDBMIDebugging.xcplugin/Contents/Resources/PBGDBIntrospectionSupport.A.dylib 0x14000 - 0x1a8ff3 +基础 ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/Foundation.framework/Foundation 0x29e000 - 0x2063fe7 +UIKit ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/UIKit.framework/UIKit 0x23be000 - 0x25c6fe7 com.apple.CoreGraphics 1.359.16 (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics 0x262e000 - 0x2665ff7 +核心位置 ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CoreLocation.framework/CoreLocation 0x268e000 - 0x2799fff +CoreFoundation ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation 0x287d000 - 0x2a15fe7 +libicucore.A.dylib ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/lib/libicucore.A.dylib 0x2a76000 - 0x2b75fe7 +libxml2.2.dylib ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/lib/libxml2.2.dylib 0x2b9a000 - 0x2c41fe7 +CFNetwork ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CFNetwork.framework/CFNetwork 0x2cc0000 - 0x2cf4ff7 +系统配置??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration 0x2d16000 - 0x2d65ff7 +安全??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/Security.framework/Security 0x2d8b000 - 0x2e58fe7 +libsqlite3.dylib ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/lib/libsqlite3.dylib 0x2e68000 - 0x2ef3fff com.apple.framework.IOKit 1.5.1 (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit 0x2f19000 - 0x2f26ff3 +图形服务 ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices 0x2f39000 - 0x3071ff7 +JavaScriptCore ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore 0x30c5000 - 0x3888fe7 +WebCore ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/WebCore.framework/WebCore 0x3db5000 - 0x3e4bff7 +WebKit ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/WebKit.framework/WebKit 0x3ec9000 - 0x3f87fe7 +ImageIO ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/ImageIO.framework/ImageIO 0x3fb2000 - 0x3fd0ff7 +地址簿 ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/AddressBook.framework/AddressBook 0x3ff1000 - 0x4145fe7 +音频工具箱 ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox 0x4198000 - 0x425aff7 +QuartzCore ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/QuartzCore.framework/QuartzCore 0x42b5000 - 0x42c2ff7 +SpringBoard 服务 ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices 0x42cd000 - 0x42f9fff +AppSupport ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/AppSupport.framework/AppSupport 0x4319000 - 0x435cfe7 +CoreText ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CoreText.framework/CoreText 0x438c000 - 0x43c7ff7 +MobileCoreServices ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/MobileCoreServices.framework/MobileCoreServices 0x43e5000 - 0x448efe7 +ProofReader ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/ProofReader.framework/ProofReader 0x44ab000 - 0x44b6ff7 +CoreVideo ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/CoreVideo.framework/CoreVideo 0x44c2000 - 0x44d8fe7 +OpenGLES ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/OpenGLES.framework/OpenGLES 0x44e2000 - 0x4558ff3 +CoreAudio ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CoreAudio.framework/CoreAudio 0x4594000 - 0x4649fe7 libcrypto.0.9.7.dylib ??? (???) /usr/lib/libcrypto.0.9.7.dylib 0x468f000 - 0x46b5fff libssl.0.9.7.dylib ??? (???) /usr/lib/libssl.0.9.7.dylib 0x46ea000 - 0x46eaff7 +AccessibilitySettingsLoader ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/AccessibilityBundles/AccessibilitySettingsLoader.bundle/AccessibilitySettingsLoader 0x46ef000 - 0x46f1ff7 +libAccessibility.dylib ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/lib/libAccessibility.dylib 0x7900000 - 0x7909ffb +IAP ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/IAP.framework/IAP 0x7916000 - 0x794cff7 +UIKit ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/AccessibilityBundles/UIKit.axbundle/UIKit 0x7990000 - 0x799bff7 +UI可访问性 ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/UIAccessibility.framework/UIAccessibility 0x79ab000 - 0x79baff7 +AXRuntime ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/AXRuntime.framework/AXRuntime 0x79c8000 - 0x7a0afe7 +libCGFreetype.A.dylib ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CoreGraphics.framework/Resources/libCGFreetype.A.dylib 0x7f31000 - 0x7f57fe7 +libRIP.A.dylib ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CoreGraphics.framework/Resources/libRIP.A.dylib 0x9cff000 - 0x9d3efe7 +TextInput ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/PrivateFrameworks/TextInput.framework/TextInput 0x9d6a000 - 0x9eafff3 +libmecabra.dylib ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/lib/libmecabra.dylib 0x9ee1000 - 0x9ff4ff7 +CoreData ??? (???) /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/System/Library/Frameworks/CoreData.framework/CoreData 0x8fe00000 - 0x8fe4162b dyld 132.1 (???) /usr/lib/dyld 0x91fe7000 - 0x91feafe7 libmathCommon.A.dylib ??? (???) /usr/lib/system/libmathCommon.A.dylib 0x93497000 - 0x93501fe7 libstdc++.6.dylib ??? (???) /usr/lib/libstdc++.6.dylib 0x937b8000 - 0x9395cfeb libSystem.B.dylib ??? (???) /usr/lib/libSystem.B.dylib 0x96692000 - 0x966d8ff7 libauto.dylib ??? (???) /usr/lib/libauto.dylib 0x9706e000 - 0x97082fe7 libbsm.0.dylib ??? (???) /usr/lib/libbsm.0.dylib 0x98055000 - 0x98102fe7 libobjc.A.dylib ??? (???) /usr/lib/libobjc.A.dylib 0x98103000 - 0x981f7fe7 libiconv.2.dylib ??? (???) /usr/lib/libiconv.2.dylib 0x98fd9000 - 0x98fe7fe7 libz.1.dylib ??? (???) /usr/lib/libz.1.dylib 0xffff0000 - 0xffff1fff libSystem.B.dylib ??? (???) /usr/lib/libSystem.B.dylib

【问题讨论】:

  • 感谢您重新格式化 dirk!

标签: objective-c xcode methods crash-reports


【解决方案1】:

这是错误的:

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的代码尝试释放对象时,它不再存在,应用程序崩溃)

【讨论】:

  • 谢谢伙计,这很奏效。我花了很长时间试图弄清楚那个东西,它出人意料地奏效了,至少你第一次尝试的时候,但是是的,它坏了。非常感谢,由于这里的两个答案,现在代码似乎可以完美运行,我希望我可以给两个答案绿色检查,但只允许一个。现在我开始尝试解决更多问题。 :)
  • 谢谢。在释放 NSXMLParser 返回的文本后,我遇到了崩溃。我的问题是,新对象返回的索引路径不是吗?方法返回时不会增加引用计数吗?
  • @JackBeNimble 返回的 indexPath 是由其他东西创建的。你不管理其他对象的东西。只释放你 alloc+init 的对象
【解决方案2】:

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

让我印象深刻的是你初始化了事务,然后立即释放 categoryString。如果您没有保留字符串,那么这可能是您崩溃的根源。

顺便说一句,您正在泄漏内存。这里:

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

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

NSString *descriptionString;
descriptionString = descriptionField.text;

或更简单地说:

NSString *descriptionString = descriptionField.text;

【讨论】:

  • 我将字符串设置为不同的属性,我正在使用复制,也许我应该使用保留? @property (nonatomic, copy) NSString *description;至于内存泄漏,谢谢,我一直在搞乱释放,因为在研究这个时我发现分段错误可能是由过度释放对象引起的。立即尝试保留
  • 我认为解决了这个问题,虽然程序仍然不能正常工作,但我认为就是这样。谢谢
  • 属性声明只影响 getter 和 setter,而不影响 init 函数,因此如果您没有在 init 方法中使用 setter 函数,问题可能仍然存在。很高兴我的解决方案有所帮助。
  • 我在 init 方法中使用了 setter
猜你喜欢
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多