NSString 溢出其缓冲区,应用程序崩溃.调试器看不到任何堆栈跟踪信息.但为什么? [英] NSString overflows its buffer and app crashed. Debugger doesn't see any stack trace information. But why?

查看:38
本文介绍了NSString 溢出其缓冲区,应用程序崩溃.调试器看不到任何堆栈跟踪信息.但为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间来找出我的应用程序崩溃的原因.在某些情况下,我的计数变量没有正确初始化.

I spent a lot of time to find out why my app crashed. My count variable doesn't properly initialized in some cases.

NSString 溢出其缓冲区,应用程序崩溃.调试器看不到任何堆栈跟踪信息.但为什么?

NSString overflows its buffer and app crashed. Debugger doesn't see any stack trace information. But why?

int count = 2147483647;
NSString *lines = @"";
for(int i = 0; i < count; i ++)
{
    lines = [NSString stringWithFormat:@"%@%@", lines, @"\n"];
}

更新:为什么调试器不显示任何堆栈跟踪信息?

UPDATE: Why doesn't debugger show any stack trace information?

推荐答案

您不仅要创建长字符串,还要创建大量字符串.每次调用 +stringWithFormat: 都会创建一个新字符串,该字符串比前一个字符长一个字符 ('\n').这些字符串是自动释放的,但自动释放池永远不会被耗尽,所以你用大量的字符串填充内存,比如 @"\n", @"\n\n"@"\n\n\n"@"\n\n\n\n",等等.在达到 NSString 中的任何内部缓冲区大小限制(如果有的话)之前很久,您可能已经崩溃了.

You're not only creating long strings, you're creating lots of strings. Every call to +stringWithFormat: creates a new string that's one character ('\n') longer than the one before. Those strings are autoreleased but the autorelease pool never gets drained, so you're filling up memory with a great many strings like @"\n", @"\n\n", @"\n\n\n", @"\n\n\n\n", and so on. You're probably crashing from that long before you reach any internal buffer size limit (if there is one) in NSString.

这篇关于NSString 溢出其缓冲区,应用程序崩溃.调试器看不到任何堆栈跟踪信息.但为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆