没有字符串格式化的NSLog导致EXC_BAD_ACCESS错误

14

我在以下代码行中遇到了EXC_BAD_ACCESS(或malloc错误):

NSLog(@"Points:");  

这对我来说毫无意义,因为它应该访问一个字符串常量而不是其他什么。通常会出现EXC_BAD_ACCESS错误,但有时也会出现以下错误(在同一行):

Annotate23D(50572,0xac6bb2c0) malloc: *** error for object 0x7051004: incorrect checksum for freed object - object was probably modified after being freed.  

我遇到了malloc错误,但无法从bad access错误中获取完整的堆栈跟踪信息。以下是malloc错误的堆栈跟踪:

#0  0x960e7c97 in malloc_error_break ()
#1  0x960a94ce in szone_error ()
#2  0x960a954e in free_list_checksum_botch ()
#3  0x960afec0 in small_malloc_from_free_list ()
#4  0x960b124c in szone_malloc_should_clear ()
#5  0x960b166b in szone_malloc ()
#6  0x960e7962 in malloc_zone_malloc ()
#7  0x960e8882 in malloc ()
#8  0x020e5837 in uhash_hashUChars ()
#9  0x020e5c01 in uhash_setResizePolicy ()
#10 0x020e5ca5 in uhash_init ()
#11 0x02075896 in uhash_open ()
#12 0x021f5978 in icu::ZoneMeta::getCanonicalCLDRID ()
#13 0x021f6a4f in icu::ZoneMeta::getCanonicalCLDRID ()
#14 0x021ab551 in icu::TimeZone::getCanonicalID ()
#15 0x021bfaf2 in ucal_getCanonicalTimeZoneID ()
#16 0x024d8bf4 in __nameStringOK ()
#17 0x024d8ae4 in -[__NSPlaceholderTimeZone __initWithName:cache:] ()
#18 0x024d89d0 in -[__NSPlaceholderTimeZone initWithName:] ()
#19 0x024d884b in +[NSTimeZone timeZoneWithName:] ()
#20 0x024d8768 in +[NSTimeZone systemTimeZone] ()
#21 0x024d84d7 in +[NSTimeZone defaultTimeZone] ()
#22 0x024d846d in CFTimeZoneCopyDefault ()
#23 0x024e6726 in CFCalendarCreateWithIdentifier ()
#24 0x02509e67 in __CFLogCString ()
#25 0x02509db3 in _CFLogvEx ()
#26 0x00b99b63 in NSLogv ()
#27 0x00b99ad5 in NSLog ()
#28 0x00008323 in -[Cylinderoid generateMesh] (self=0x6a73fa0, _cmd=0x6e836) at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/Cylinderoid.mm:154
#29 0x0006cd85 in -[MeshGenerator rendererForObjects:] (self=0x6a67b80, _cmd=0x6e246, workspace=0xa83f270) at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/MeshGenerator.m:16
#30 0x00005367 in -[WorkspaceViewController renderButton:] (self=0xa8377f0, _cmd=0x6e4ac, sender=0x6a58260) at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/WorkspaceViewController.m:304
#31 0x02562ec9 in -[NSObject performSelector:withObject:withObject:] ()
#32 0x002365c2 in -[UIApplication sendAction:to:from:forEvent:] ()
#33 0x0023655a in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
#34 0x002dbb76 in -[UIControl sendAction:to:forEvent:] ()
#35 0x002dc03f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#36 0x002db2fe in -[UIControl touchesEnded:withEvent:] ()
#37 0x004f4a2a in _UIGestureRecognizerUpdate ()
#38 0x025359ce in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#39 0x024cc670 in __CFRunLoopDoObservers ()
#40 0x024984f6 in __CFRunLoopRun ()
#41 0x02497db4 in CFRunLoopRunSpecific ()
#42 0x02497ccb in CFRunLoopRunInMode ()
#43 0x0244a879 in GSEventRunModal ()
#44 0x0244a93e in GSEventRun ()
#45 0x00233a9b in UIApplicationMain ()
#46 0x00002b88 in main (argc=1, argv=0xbffff590) at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/main.m:1  

值得注意的是,所讨论的文件是启用了ARC的Objective-C++文件。该方法本身非常庞大,因此我将其放在这里以保持问题简短。问题出在第94行。如何修复或调试都非常感谢。提前致谢!

快速编辑:

现在我还遇到了页面损坏错误。从调试器中看到:

Annotate23D(50697) malloc: protecting edges
Annotate23D(50697) malloc: recording malloc stacks to disk using standard recorder
Annotate23D(50697) malloc: process 50678 no longer exists, stack logs deleted from /tmp/stack-logs.50678.Annotate23D.QDMh8a.index
Annotate23D(50697) malloc: stack logs being written into /tmp/stack-logs.50697.Annotate23D.XuPnEH.index
objc[50697]: autorelease pool page 0x7239000 corrupted
  magic 0 3f473a55 3f20c2f1 0
  pthread 0x43af3ce7

(gdb) bt
#0  0x026f28e5 in _objc_trap ()
#1  0x026f296e in _objc_fatal ()
#2  0x02706960 in objc_autoreleasePoolPush ()
#3  0x0248a95a in _CFAutoreleasePoolPush ()
#4  0x00b24e2b in NSPushAutoreleasePool ()
#5  0x00b99af4 in NSLogv ()
#6  0x00b99ad5 in NSLog ()
#7  0x00008323 in -[Cylinderoid generateMesh] (self=0x6ddd8a0, _cmd=0x6e836) at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/Cylinderoid.mm:151
#8  0x0006cd85 in -[MeshGenerator rendererForObjects:] (self=0x6a9de30, _cmd=0x6e246, workspace=0x6dc1460) at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/MeshGenerator.m:16
#9  0x00005367 in -[WorkspaceViewController renderButton:] (self=0x6db8a30, _cmd=0x6e4ac, sender=0x6dbd6a0) at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/WorkspaceViewController.m:304
#10 0x02562ec9 in -[NSObject performSelector:withObject:withObject:] ()
#11 0x002365c2 in -[UIApplication sendAction:to:from:forEvent:] ()
#12 0x0023655a in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
#13 0x002dbb76 in -[UIControl sendAction:to:forEvent:] ()
#14 0x002dc03f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#15 0x002db2fe in -[UIControl touchesEnded:withEvent:] ()
#16 0x004f4a2a in _UIGestureRecognizerUpdate ()
#17 0x025359ce in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#18 0x024cc670 in __CFRunLoopDoObservers ()
#19 0x024984f6 in __CFRunLoopRun ()
#20 0x02497db4 in CFRunLoopRunSpecific ()
#21 0x02497ccb in CFRunLoopRunInMode ()
#22 0x0244a879 in GSEventRunModal ()
#23 0x0244a93e in GSEventRun ()
#24 0x00233a9b in UIApplicationMain ()
#25 0x00002b88 in main (argc=1, argv=0xbffff590) at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/main.m:16
(gdb) info malloc-history 0x7239000
Alloc: Block address: 0x07239000 length: 4096
Stack - pthread: 0xac6bb2c0 number of frames: 4
    0: 0x960e76ee in malloc_zone_memalign
    1: 0x270697d in objc_autoreleasePoolPush
    2: 0x2b22 in main at /Users/haldean/Code/ipad/Annotate23D/Annotate23D/main.m:16
    3: 0x2ae5 in start

1
你不可能在NSLog上崩溃。我认为在这行代码之前或之后有一个导致崩溃的函数调用,程序只是在这一行显示出来了。有没有函数调用? - Shubhank
问题中有一个 pastebin 的链接,其中包含相关的方法。在前一行,我对几个东西调用了 free。但是如果你查看堆栈跟踪,崩溃肯定是在 NSLog 内部发生的。https://gist.github.com/1998932 - Haldean Brown
你尝试过在这个项目上运行分析器吗? - ThomasW
是的,它没有显示任何奇怪的东西。没有僵尸进程,这就是我想象中的问题。 - Haldean Brown
1
result->data = (GLfloat*) malloc(result->size * 6); 看起来有问题 - 我认为你应该乘以 sizeof(GLfloat) - pmdj
显示剩余3条评论
4个回答

39

这是一个老问题,但在Swift中,如果您记录了一个包含百分号(%)的编码URL,就会遇到此问题-例如:

NSLog("My long Encoded URL: \(myLongUrlVar)")

相反,它将使用 params:

NSLog("My long Encoded URL: %@", myLongUrlVar)

4
哇!这对我有帮助。谢谢! - devxoul
谢谢 - 我也有同样的问题! - BTSmith
太有道理了 :) - Lasse Bunk
2
这是我的问题。我一直在使用NSLog(message),改为NSLog("%@", message)后修复了EXE_BAD_ACCESS错误。 - JonnyB

4
在评论中,我建议:
问题不在于NSLog。问题可能是某些内存被破坏了,这导致了NSLog崩溃。如果您能拆分该方法以使其更易于阅读,甚至可以将功能分离成一个新对象,那么这可能会有所帮助。此外,避免使用魔术数字(3和6)。
看起来@haldean采纳了这个建议并跟踪了问题。我不能声称自己实际上解决了跟踪问题的艰难工作,但我很高兴这个建议有所帮助。

2

字符串中有%,将%替换为%%

NSLog([message stringByReplacingOccurrencesOfString:@"%" withString:@"%%"]);

0

首先我会尝试清空构建目录并进行全新的构建(在猎杀僵尸之后或在这种情况下之前)。有时,之前构建留下的垃圾可能会导致奇怪的问题。


刚刚尝试了清理和重建,但没有解决任何问题。我在我的方案中打开了Zombies选项,并尝试通过Zombies工具运行它,但都没有发现任何问题 - 你知道其他的寻找僵尸对象的方法吗? - Haldean Brown

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接