2013-01-15 100 views
0

我有一個對象,類似於UIAlertView,它顯示一個帶有自定義消息和標題的對話框。到目前爲止,下面的代碼工作,以顯示該對話框:iOS - 防止對象釋放

DialogBox *dialog = [[DialogBox alloc] initWithMessage:@"Hello" delegate:self]; 
[dialog showDialog]; 

當我點擊無論是「YES」或「NO」對話框上的按鈕,但是,應用程序崩潰,因爲DialogBox實例已被釋放。

使*對話框實例變量屬性修復了這個問題,但這對我不起作用,因爲我希望能夠隨機創建DialogBox實例變量。

有沒有辦法讓* dialog對象實例變量「alive」,直到我希望它真的從視圖中消失?

任何幫助將不勝感激。

Crash log below: 

Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x000000005f7b5e7d 

VM Regions Near 0x5f7b5e7d: 
    MALLOC_TINY   000000000ea00000-000000000eb00000 [ 1024K] rw-/rwx SM=PRV 
--> 
    __TEXT     000000008fea8000-000000008fedb000 [ 204K] r-x/rwx SM=COW /usr/lib/dyld 

Application Specific Information: 
objc_msgSend() selector name: performSelector:withObject:withObject: 
iPhone Simulator 369.2, iPhone OS 6.1 (iPhone/10B5126b) 


Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 
0 libobjc.A.dylib     0x010e60b0 objc_msgSend + 36 
1 UIKit       0x0001c2c0 -[UIApplication sendAction:to:from:forEvent:] + 96 
2 UIKit       0x0001c258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 
3 UIKit       0x000dd021 -[UIControl sendAction:to:forEvent:] + 66 
4 UIKit       0x000dd57f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578 
5 UIKit       0x000dc6e8 -[UIControl touchesEnded:withEvent:] + 546 
6 UIKit       0x0004bcef -[UIWindow _sendTouchesForEvent:] + 846 
7 UIKit       0x0004bf02 -[UIWindow sendEvent:] + 273 
8 UIKit       0x00029d4a -[UIApplication sendEvent:] + 436 
9 UIKit       0x0001b698 _UIApplicationHandleEvent + 9874 
10 GraphicsServices    0x01bf2df9 _PurpleEventCallback + 339 
11 GraphicsServices    0x01bf2ad0 PurpleEventCallback + 46 
12 CoreFoundation     0x01c0cbf5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53 
13 CoreFoundation     0x01c0c962 __CFRunLoopDoSource1 + 146 
14 CoreFoundation     0x01c3dbb6 __CFRunLoopRun + 2118 
15 CoreFoundation     0x01c3cf44 CFRunLoopRunSpecific + 276 
16 CoreFoundation     0x01c3ce1b CFRunLoopRunInMode + 123 
17 GraphicsServices    0x01bf17e3 GSEventRunModal + 88 
18 GraphicsServices    0x01bf1668 GSEventRun + 104 
19 UIKit       0x00018ffc UIApplicationMain + 1211 
20 DialogApp      0x0000257d main + 141 (main.m:16) 
21 DialogApp      0x000024a5 start + 53 

Thread 1: 
0 libsystem_kernel.dylib   0x998580ee __workq_kernreturn + 10 
1 libsystem_c.dylib    0x9214904c _pthread_workq_return + 45 
2 libsystem_c.dylib    0x92148e19 _pthread_wqthread + 448 
3 libsystem_c.dylib    0x92130cca start_wqthread + 30 

Thread 2:: Dispatch queue: com.apple.libdispatch-manager 
0 libsystem_kernel.dylib   0x998589ca kevent64 + 10 
1 libdispatch.dylib    0x049fec2b _dispatch_mgr_invoke + 863 
2 libdispatch.dylib    0x049fe8cc _dispatch_mgr_thread + 61 

Thread 3: 
0 libsystem_kernel.dylib   0x998580ee __workq_kernreturn + 10 
1 libsystem_c.dylib    0x9214904c _pthread_workq_return + 45 
2 libsystem_c.dylib    0x92148e19 _pthread_wqthread + 448 
3 libsystem_c.dylib    0x92130cca start_wqthread + 30 

Thread 4: 
0 libsystem_kernel.dylib   0x998580ee __workq_kernreturn + 10 
1 libsystem_c.dylib    0x9214904c _pthread_workq_return + 45 
2 libsystem_c.dylib    0x92148e19 _pthread_wqthread + 448 
3 libsystem_c.dylib    0x92130cca start_wqthread + 30 

Thread 5:: WebThread 
0 libsystem_kernel.dylib   0x998557d2 mach_msg_trap + 10 
1 libsystem_kernel.dylib   0x99854cb0 mach_msg + 68 
2 CoreFoundation     0x01c38a49 __CFRunLoopServiceMachPort + 185 
3 CoreFoundation     0x01c3d8d4 __CFRunLoopRun + 1380 
4 CoreFoundation     0x01c3cf44 CFRunLoopRunSpecific + 276 
5 CoreFoundation     0x01c3ce1b CFRunLoopRunInMode + 123 
6 WebCore       0x0356dc50 RunWebThread(void*) + 608 
7 libsystem_c.dylib    0x92146557 _pthread_start + 344 
8 libsystem_c.dylib    0x92130cee thread_start + 34 

Thread 0 crashed with X86 Thread State (32-bit): 
    eax: 0x5f7b5e7d ebx: 0x0001c26e ecx: 0x00cb6b07 edx: 0x00005240 
    edi: 0x012cfd4f esi: 0x694c534e ebp: 0xbfffe108 esp: 0xbfffe0d4 
    ss: 0x00000023 efl: 0x00010206 eip: 0x010e60b0 cs: 0x0000001b 
    ds: 0x00000023 es: 0x00000023 fs: 0x00000000 gs: 0x0000000f 
    cr2: 0x5f7b5e7d 
Logical CPU: 1 
+0

崩潰發生在哪裏?在委託方法中? –

+0

這些按鈕具有指向DialogBox類中方法的選擇器,並且由於對象已被釋放,因此在調用這些方法時看起來有錯誤。 – achiral

+1

你可以把崩潰日誌? –

回答

2

這是您想保留對self的引用以防止釋放的情況之一(相對較少)。這會讓你的對話框「火併且忘記」,而不會被釋放。

在你DialogBox類,添加屬性(它應該是一個私人的),您可以存儲一個參考。我們稱之爲selfProperty

然後,當您初始化您的班級時,請使用self.selfProperty = self;,並在您想要釋放時將其設置爲零。

+0

感謝您的回覆。我添加了一個'@property(nonatomic,strong)id selfProperty'屬性,當我初始化DialogBox時,我將其設置爲'self',但是當我選擇其中一個對話框按鈕時,仍會出現取消分配錯誤。 – achiral

+0

@lnafziger爲什麼一個財產,而不是一個伊娃? – Richard

+0

@achiral:你確定你的初始化函數被調用了嗎?設置一個斷點並確保。 – lnafziger