NSLog上的EXC_BAD_ACCESS没有字符串格式

Hal*_*own 11 cocoa-touch memory-management objective-c objective-c++

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

NSLog(@"Points:");  
Run Code Online (Sandbox Code Playgroud)

这对我来说没有任何意义,因为它应该访问一个字符串常量而不是别的.它通常是一个EXC_BAD_ACCESS,但它偶尔会出现以下错误(在同一行):

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

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  
Run Code Online (Sandbox Code Playgroud)

可能相关的是,有问题的文件是启用了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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ark 35

旧问题,但在Swift中,如果您正在记录包含'%'的编码URL,则会遇到此问题 - 例如:

NSLog("My long Encoded URL: \(myLongUrlVar)")
Run Code Online (Sandbox Code Playgroud)

相反,它将与params一起使用:

NSLog("My long Encoded URL: %@", myLongUrlVar)
Run Code Online (Sandbox Code Playgroud)

  • 我的天啊.这对我有帮助.谢谢! (4认同)
  • 这是我的问题。正在使用 `NSLog(message)`,更改为 `NSLog("%@", message)` 修复了 EXE_BAD_ACCESS 错误。 (2认同)

Tho*_*asW 4

在评论中我建议:

问题不在于NSLog. 问题可能是某些内存被破坏,从而导致NSLog. 如果您可以分解您正在使用的方法以使其更易于阅读,甚至可能将功能分离到一个新对象中,这可能会有所帮助。另外,避免使用幻数(3 和 6)。

看来 @haldean 采纳了这个建议并找到了问题所在。我不能声称自己确实做了艰苦的工作来追踪问题,但我很高兴这个建议有所帮助。