代码之家  ›  专栏  ›  技术社区  ›  lostInTransit

iPhone开发-内存释放问题

  •  5
  • lostInTransit  · 技术社区  · 16 年前

    我遇到了释放一个已经释放的对象的问题,但是我一辈子都找不到错误发生的地方。我添加了nszombieEnabled标志,这是我在gdb中得到的日志。有人能告诉我如何解决这个问题,或者更确切地说,找出错误发生的位置吗?

    *** -[CFString release]: message sent to deallocated instance 0x5e4780 
    (gdb) where
    #0  0x952ff907 in ___forwarding___ ()
    #1  0x952ffa12 in __forwarding_prep_0___ ()
    #2  0x9260e20f in NSPopAutoreleasePool ()
    #3  0x30a564b0 in _UIApplicationHandleEvent ()
    #4  0x31563dea in SendEvent ()
    #5  0x3156640c in PurpleEventTimerCallBack ()
    #6  0x95280615 in CFRunLoopRunSpecific ()
    #7  0x95280cf8 in CFRunLoopRunInMode ()
    #8  0x31564600 in GSEventRunModal ()
    #9  0x315646c5 in GSEventRun ()
    #10 0x30a4ec98 in -[UIApplication _run] ()
    #11 0x30a5a094 in UIApplicationMain ()
    #12 0x00002494 in main (argc=1, argv=0xbfffef9c) at /Users/adminUser/Projects/MyProject/main.m:14
    

    谢谢。

    1 回复  |  直到 13 年前
        1
  •  16
  •   Community CDub    13 年前

    自动释放池正在尝试释放已释放的对象。

    如果手动释放为自动释放注册的对象,则可能发生这种情况。

    NSString* foo = [NSString stringWithFormat:@"foo:%d",42];  
    [foo release];  /* this release is bad, as this object is already 
    registered for autorelease */
    

    您可以使用以下方法查找分配点:

    1. 集合 MallocStackLogging, MallocStackLoggingNoCompact 环境设置为1。
    2. 运行程序,一旦它进入gdb使用 malloc_history 从shell中查找分配的堆栈跟踪: malloc_history <pid> <addr> . (NszombieEnabled将在gdb中打印出地址)

    另一种选择(不太可能降低运行时性能)是将仪器工具与“僵尸”模板一起使用。它将跟踪僵尸,并告诉您僵尸的历史,而不必使用malloc_history命令。