AutoreleasePool icon indicating copy to clipboard operation
AutoreleasePool copied to clipboard

貌似代码有问题,测试环境Xcode 9 场景1,2,3输出的结果都是string: leichunfeng并没有nil值

Open ios-zhouyu opened this issue 8 years ago • 1 comments

场景一: 2017-10-14 22:10:41.680201+0800 autorelease[7273:146176] string: leichunfeng 2017-10-14 22:10:41.680495+0800 autorelease[7273:146176] string: leichunfeng 2017-10-14 22:10:41.688007+0800 autorelease[7273:146176] string: leichunfeng

场景二: 2017-10-14 22:12:50.991138+0800 autorelease[7349:149891] string: leichunfeng 2017-10-14 22:12:50.991394+0800 autorelease[7349:149891] string: leichunfeng 2017-10-14 22:12:50.996902+0800 autorelease[7349:149891] string: leichunfeng

场景三 2017-10-14 22:14:37.813182+0800 autorelease[7413:153177] string: leichunfeng 2017-10-14 22:14:37.813469+0800 autorelease[7413:153177] string: leichunfeng 2017-10-14 22:14:37.818701+0800 autorelease[7413:153177] string: leichunfeng

image

image

ios-zhouyu avatar Oct 14 '17 14:10 ios-zhouyu

iOS 64 位系统引入了 Tagged Pointer 技术,在 64 位系统上,本例中生成的字符串是 NSTaggedPointerString 类型的,其 retain count 为 2^63-1 ,会一直存在于栈区,所以打印时一直有值。

可以把 @"leichunfeng" 改成中文字符,比如 @"雷纯峰" ,避免系统使用Tagged Pointer 技术。

参考链接: http://blog.devtang.com/2014/05/30/understand-tagged-pointer/
https://www.mikeash.com/pyblog/friday-qa-2015-07-31-tagged-pointer-strings.html

Huang-Libo avatar Dec 27 '19 07:12 Huang-Libo