dataUsingEncoding是否可能导致崩溃?

时间:2019-05-05 10:13:58

标签: objective-c

当它进入NSString dataUsingEncoding方法时,崩溃很少发生。

源代码:

NSData *latin1Data = [appName dataUsingEncoding:NSUTF8StringEncoding];

调用堆栈:

MACH_Exception Crashed with mach exception EXC_BAD_ACCESS

Thread 0 name: (null)
0    libobjc.A    objc_object::release() (in libobjc.A.dylib)
1    CoreFoundation    _common_removeAllObjects (in CoreFoundation) 188
2    CoreFoundation    -[__NSArrayM dealloc] (in CoreFoundation) 28

3    libobjc.A    (anonymous namespace)::AutoreleasePoolPage::pop(void*) (in libobjc.A.dylib) 704
4    libdispatch    __dispatch_root_queue_drain (in libdispatch.dylib) 1148
5    libdispatch    __dispatch_worker_thread3 (in libdispatch.dylib) 124
6    libsystem_pthread    __pthread_wqthread (in libsystem_pthread.dylib) 1288
7    libobjc.A    _objc_msgSend (in libobjc.A.dylib) 40
8    Foundation    -[NSConcreteMutableData initWithLength:] (in Foundation) 316
9    Foundation    -[NSString(NSStringOtherEncodings) dataUsingEncoding:allowLossyConversion:] (in Foundation)
10  xxxApp    -[ xxxClass xxxMethod]  xxxFile.mm line:300
xxxFile.mm的

第300行是:

NSData *latin1Data = [appName dataUsingEncoding:NSUTF8StringEncoding];

编辑,从评论中复制:

295 -(NSString *)userAgentString {
296     NSBundle *bundle = [NSBundle bundleForClass:[self class]];
297     // Attempt to find a name for this application
298     NSString *appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"];
299
300     NSData *latin1Data = [appName dataUsingEncoding:NSUTF8StringEncoding];

在调用函数userAgentString时在第300行崩溃。

1 个答案:

答案 0 :(得分:0)

您应该从应用程序的mailBundle中获取该信息。

NSString *buildNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]

p.s: 我惊讶于私人类的作品。似乎您偶尔会得到零参考。
(在这里猜测:您的类也有一个初始化。如果没有及时完成,则运行时将不知道该类所属的包?)