NSLog上的EXC_BAD_ACCESS没有字符串格式

时间:2012-03-08 05:36:21

标签: objective-c cocoa-touch memory-management objective-c++

我在以下代码行中收到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错误的堆栈跟踪(我很难从错误的访问错误中获取完整的错误)是:

#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 ++文件。方法本身是巨大的,所以我把它here放在一边以保持问题的简短。有问题的行是第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

4 个答案:

答案 0 :(得分:34)

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

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

相反,它适用于params:

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

答案 1 :(得分:4)

在我建议的评论中:

  

问题不在于NSLog。问题可能是某些内存被粉碎,导致NSLog崩溃。如果您可以分解您正在使用的方法以使其更易于阅读,甚至可能将功能分离为新对象,这可能会有所帮助。另外,请避免使用幻数(3& 6)。

似乎@haldean采用了这个建议并追踪了问题。我无法为追查问题而努力工作,但我很高兴这个建议有所帮助。

答案 2 :(得分:0)

我会尝试的第一件事(在僵尸狩猎之后或者在这种情况下之前)是清除构建目录并进行新的重建。有时遗留在以前的版本中会导致奇怪的问题。

答案 3 :(得分:0)

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

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