【问题标题】:Crash while converting html to string in UILabel in IOS在IOS的UILabel中将html转换为字符串时崩溃
【发布时间】:2016-02-20 02:24:14
【问题描述】:

我有主页/视图,我在列表视图中显示所有记录(仅标题和日期)。

当我点击任何记录时,我会在另一个页面中显示记录的完整信息。 每条记录都有一个需要显示的html字符串值。

为了显示 html 内容,我使用 UILabel 控件,它可以正确显示。

但是当导航在 2 个页面/视图之间来回完成时,它会崩溃。 崩溃报告指向与问题相关的内存。

modifiedContent = [modifiedContent stringByAppendingString:[NSString stringWithFormat:@"<style>body{font-family: '%@'; font-size:%fpx;}</style>",@"Helvetica",17.0f]];

content.attributedText = [[NSAttributedString alloc] initWithData:[modifiedContent dataUsingEncoding:NSUnicodeStringEncoding]
                              options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                              documentAttributes:nil
                              error:nil];

是否需要更改上述代码以处理内存泄漏或是否需要不同的编码类型。

需要从IOS 7支持到IOS Current Version

下面是堆栈跟踪

MyAppName(1098,0x39d7000) malloc: *** mach_vm_map(size=151552) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
MyAppName(1098,0x39d7000) malloc: *** mach_vm_map(size=151552) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
2015-11-19 10:18:59.643 MyAppName[1098:436403] -[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x76954080
2015-11-19 10:18:59.646 MyAppName[1098:436403] -[NSCoder(CACoderAdditions) CA_encodeObject:forKey:conditional:]: ignoring exception -[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x76954080
2015-11-19 10:18:59.650 MyAppName[1098:436403] *** NSKeyedArchiver warning: replacing existing value for key ''; probable duplication of encoding keys in class hierarchy
2015-11-19 10:18:59.660 MyAppName[1098:436403] *** NSKeyedArchiver warning: replacing existing value for key ''; probable duplication of encoding keys in class hierarchy
2015-11-19 10:18:59.662 MyAppName[1098:436403] *** NSKeyedArchiver warning: replacing existing value for key ''; probable duplication of encoding keys in class hierarchy
2015-11-19 10:18:59.675 MyAppName[1098:436403] *** NSKeyedArchiver warning: replacing existing value for key ''; probable duplication of encoding keys in class hierarchy
2015-11-19 10:18:59.677 MyAppName[1098:436403] *** NSKeyedArchiver warning: replacing existing value for key ''; probable duplication of encoding keys in class hierarchy
2015-11-19 10:18:59.785 MyAppName[1098:436403] *** NSKeyedArchiver warning: replacing existing value for key ''; probable duplication of encoding keys in class hierarchy
(lldb)

【问题讨论】:

  • 这些“内存相关”问题是什么?崩溃发生在哪里?异常(如果有的话)说明了什么?代码是使用 ARC 编译的吗?如果不是,则存在内存泄漏。
  • 我使用的 Xcode 版本是 8.3,iphone 版本是 9.1,因此我无法直接从 xcode 运行代码。该项目不是ARC编译的。我已经编辑了我的问题,其中包含从 Crittercism 生成的堆栈跟踪

标签: html ios objective-c ios7 uilabel


【解决方案1】:

由于您声明代码不使用 ARC,因此每次运行引用的代码时都会泄漏 NSAttributedString 实例。要解决这个问题,您可以这样做(我在这里使用非正统格式以使其在 SO 上更具可读性;请根据您的编码风格调整它):

NSAttributedString *attributed = [[NSAttributedString alloc]
    initWithData:[modifiedContent dataUsingEncoding:NSUTF8StringEncoding]        
    options:@{
        NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
    }                          
    documentAttributes:nil                          
    error:nil];
content.attributedText = attributed;
[attributed release];

(另外:使用 NSUTF8StringEncoding 而不是 NSUnicodeStringEncoding 或至少与您使用的 which 保持一致;您已使用一种编码创建数据,但在选项中告知它将是在另一种编码中。)

【讨论】:

  • 抱歉代码使用了 ARC,我已将编码更改为 NSUTF8StringEncoding。但从崩溃来看,内存似乎正在泄漏。即使使用 ARC,如何释放内存。
  • 那么泄漏肯定在其他地方。使用带有 Leaks Tool 的 Instruments 来找到它。
猜你喜欢
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2020-04-21
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多