2009-01-27 102 views
5

我遇到了釋放一個已經發布的對象的這個問題,但不能爲我的生活找出錯誤發生的地方。我添加了NSZombieEnabled標誌,這是我在gdb中獲得的日誌。有人可以告訴我如何解決這個問題,或者找出錯誤發生的位置。iPhone開發 - 內存釋放問題

*** -[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 

謝謝。

回答

16

autorelease池試圖釋放已釋放的對象。

這可能發生,如果你手動釋放的自動釋放註冊的對象

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,從shell中使用malloc_history找出分配的堆棧跟蹤:malloc_history <pid> <addr>。 (NSZombieEnabled將打印出gdb中的地址)

另一種選擇(不太可能降低運行時性能)是使用帶有「Zombies」模板的Instruments工具。它會跟蹤殭屍,並告訴你一個殭屍的歷史,而不必使用malloc_history命令。

+0

謝謝。再次通過內存管理文檔。我試圖釋放一個被另一個對象使用的對象。 – lostInTransit 2009-01-27 07:39:33