2012-02-06 43 views
2

當我運行應用程序並選擇一行時,它將一些信息傳遞給EmployeeInfoPage並且工作正常。當我回擊,然後選擇相同的行。它在行tableController.empInfo = employee;上崩潰。我得到一個EXC_BAD_ACCESS錯誤當同一行被選中兩次時,UITableView崩潰

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
EmployeeInfoTableView *tableController = [[EmployeeInfoTableView alloc] init]; 

NSDictionary *employee = [[self.orderedSections valueForKey:[[[self.orderedSections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 

tableController.empInfo = employee;  

[self.navigationController pushViewController:tableController animated:YES];  

[tableController release]; 
} 

這是EmployeeInfoTableView.h文件

@interface EmployeeInfoTableView : UITableViewController <UIActionSheetDelegate , MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>{ 

    NSDictionary *empInfo; 
    NSString *currentPhoneNumber; 
    NSString *currentEmail; 

    UIActionSheet *callConfirm; 
} 

然後我用empInfo設置在tableview中的某些字段。

這是我的dealloc方法

- (void)dealloc{ 
    [super dealloc]; 
    [empInfo dealloc]; 
    [currentEmail dealloc]; 
    [currentPhoneNumber dealloc]; 
    [callConfirm dealloc]; 
} 

是我的錯誤監守我要釋放所有我的對象?

這是我從我的iPhone上得到的錯誤,我真的不知道該如何閱讀。

Exception Type: EXC_BAD_ACCESS (SIGSEGV) 
Exception Codes: KERN_INVALID_ADDRESS at 0xb001fb49 
Crashed Thread: 0 

Thread 0 name: Dispatch queue: com.apple.main-thread 
Thread 0 Crashed: 
0 libobjc.A.dylib     0x31798c98 objc_msgSend + 16 
1 libobjc.A.dylib     0x317a24d2 objc_setProperty_non_gc + 62 
2 libobjc.A.dylib     0x3179a040 objc_setProperty + 20 
3 CorporateDirectory    0x0000e5f0 0x1000 + 54768 
4 CorporateDirectory    0x00003ac2 0x1000 + 10946 
5 UIKit       0x32cc3514 -[UITableView           _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 656 
6 UIKit       0x32d270e4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 124 
7 Foundation      0x338266ce __NSFireDelayedPerform + 362 
8 CoreFoundation     0x345f4a40  __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 8 
9 CoreFoundation     0x345f6ec4 __CFRunLoopDoTimer + 844 
10 CoreFoundation     0x345f783e __CFRunLoopRun + 1082 
11 CoreFoundation     0x34587ebc CFRunLoopRunSpecific + 224 
12 CoreFoundation     0x34587dc4 CFRunLoopRunInMode + 52 
13 GraphicsServices    0x322b2418 GSEventRunModal + 108 
14 GraphicsServices    0x322b24c4 GSEventRun + 56 
15 UIKit       0x32c9dd62 -[UIApplication _run] + 398 
16 UIKit       0x32c9b800 UIApplicationMain + 664 
17 CorporateDirectory    0x00002a52 0x1000 + 6738 
18 CorporateDirectory    0x00002a10 0x1000 + 6672 




Thread 1: 
0 libsystem_kernel.dylib   0x339663ec __workq_kernreturn + 8 
1 libsystem_c.dylib    0x3480f6d8 _pthread_wqthread + 592 
2 libsystem_c.dylib    0x3480fbbc start_wqthread + 0 

Thread 2 name: Dispatch queue: com.apple.libdispatch-manager 
Thread 2: 
0 libsystem_kernel.dylib   0x33966fbc kevent + 24 
1 libdispatch.dylib    0x322df032 _dispatch_mgr_invoke + 706 
2 libdispatch.dylib    0x322e003a _dispatch_queue_invoke + 86 
3 libdispatch.dylib    0x322df5ea _dispatch_worker_thread2 + 186 
4 libsystem_c.dylib    0x3480f58a _pthread_wqthread + 258 
5 libsystem_c.dylib    0x3480fbbc start_wqthread + 0 

Thread 3 name: WebThread 
Thread 3: 
0 libsystem_kernel.dylib   0x33963c00 mach_msg_trap + 20 
1 libsystem_kernel.dylib   0x33963758 mach_msg + 44 
2 CoreFoundation     0x345f52b8 __CFRunLoopServiceMachPort + 88 
3 CoreFoundation     0x345f7562 __CFRunLoopRun + 350 
4 CoreFoundation     0x34587ebc CFRunLoopRunSpecific + 224 
5 CoreFoundation     0x34587dc4 CFRunLoopRunInMode + 52 
6 WebCore       0x3666927e _ZL12RunWebThreadPv + 382 
7 libsystem_c.dylib    0x3480e30a _pthread_start + 242 
8 libsystem_c.dylib    0x3480fbb4 thread_start + 0 

Thread 0 crashed with ARM Thread State: 
r0: 0x0016c020 r1: 0x32f63814  r2: 0x000000a8  r3: 0x0016c020 
r4: 0xb001fb41 r5: 0x00000000  r6: 0x001eeca8  r7: 0x2fdfe8c4 
r8: 0x00a1da00 r9: 0x0016c020  r10: 0x00000003  r11: 0x00161a50 
ip: 0x00000000 sp: 0x2fdfe8a4  lr: 0x317a24d9  pc: 0x31798c98 

CPSR:0x00000030

+0

這是什麼'CorporateDirectory'類你的嗎? – Rog 2012-02-06 04:07:07

回答

0

嘗試通過從didSelectRowAtIndexPath方法刪除此行

[tableController release]; 

。當我們嘗試訪問以前發佈的變量時,會引發EXC_BAD_ACCESS錯誤。您同時發佈NSDictionary *employee對象tableController

編輯:爲了防止你的字典被釋放,你可以copy字典所需的內容爲tableController.empInfo = [employee copy];,然後發佈tableController這將最適合這種情況。

+0

修復它謝謝。所以我仍然有點困惑,爲什麼重新放在它導致這個錯誤。我發佈了它,以便我可以重用它,但從我的理解我不需要釋放它,因爲如果他們點擊另一行,我將再次調用它。這個假設是否正確? – zach 2012-02-06 04:58:19

+0

發佈物品是一個好習慣,但我們必須小心。 '[tableController release];'是我們應該做的事情,以確保內存不被浪費。不幸的是,在你的情況下,你無意中釋放了你以後不得不使用的對象的內存。一種方法是使用'autorelease'。另一種是將你的對象的內容「複製」到「員工」中。 – tipycalFlow 2012-02-06 05:05:04

+0

這很有意義,謝謝 – zach 2012-02-06 05:10:26

2

我沒有看到你的didSelectRowAtIndexPath有什麼問題,但你的dealloc方法肯定是不正確的。 從不在你的代碼中調用dealloc(即只有在你完成它們之後才釋放對象)。

你的dealloc方法應該是這樣的:

- (void)dealloc{ 
    [empInfo release]; 
    [currentEmail release]; 
    [currentPhoneNumber release]; 
    [callConfirm release]; 
    [super dealloc]; 
} 
+0

如果super dealloc位於列表的開頭或結尾,那麼區別是什麼? – zach 2012-02-06 04:54:11

+0

我不確定是否有任何不同,但將它放在最後並不合邏輯,以確保所有屬性都有機會釋放,因爲此對象在調用之後會消失。同樣,我總是在開始時放置[super viewDidLoad]或[super loadView],以確保在我修改任何內容之前正確設置了視圖。 – jasondinh 2012-02-06 10:01:46

+0

@zach我不認爲他**叫**'dealloc'!沒有錯...... – tipycalFlow 2012-02-06 12:12:59