2010-09-12 107 views
2

我試圖從我的應用程序委託中運行一個方法,該方法將對象保存在其他類中,以及使計時器無效。我設置了它,以便當我的應用程序退出時,它會在我的課程中運行該方法並保存並停止計時器。iPhone:從應用程序委託崩潰導致NSTimer無效應用程序

在應用程序的委託:

- (void)applicationWillResignActive:(UIApplication *)application { 

    // Save the mission because they are leaving the app 
    if ([timeRun hasScore]) { 
     [timeRun resetWithSave]; 
    } 

} 

它在 「timeRun」 類調用的方法:

- (void)resetWithSave { 
    // Save 
    self.counterInt = 150; 
    self.timer.text = [self timeInSeconds:counterInt]; 
    [start setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; 
    self.started = NO; 
    self.score.text = @"0"; 
    [self saveMission]; 
    if ([countTimer isValid]) { 
     [countTimer invalidate]; 
    } 
    [table scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; 
} 

但是,我越來越崩潰:

2010-09-11 19:35:21.503 Score Card[2747:307] -[__NSCFType isValid]: unrecognized selector sent to instance 0x19e190 
2010-09-11 19:35:21.594 Score Card[2747:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType isValid]: unrecognized selector sent to instance 0x19e190' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x30897ed3 __exceptionPreprocess + 114 
    1 libobjc.A.dylib      0x3002f811 objc_exception_throw + 24 
    2 CoreFoundation      0x30899683 -[NSObject(NSObject) doesNotRecognizeSelector:] + 102 
    3 CoreFoundation      0x308411d9 ___forwarding___ + 508 
    4 CoreFoundation      0x30840f90 _CF_forwarding_prep_0 + 48 
    5 Score Card       0x00006b3d -[TimeRun resetWithSave] + 272 
    6 Score Card       0x00002b39 -[Score_CardAppDelegate applicationWillResignActive:] + 80 
    7 UIKit        0x31ea6879 -[UIApplication _setActivated:] + 212 
    8 UIKit        0x31eda4ab -[UIApplication _handleApplicationSuspend:eventInfo:] + 238 
    9 UIKit        0x31eab301 -[UIApplication handleEvent:withNewEvent:] + 2200 
    10 UIKit        0x31eaa901 -[UIApplication sendEvent:] + 44 
    11 UIKit        0x31eaa337 _UIApplicationHandleEvent + 5110 
    12 GraphicsServices     0x31e4504b PurpleEventCallback + 666 
    13 CoreFoundation      0x3082cce3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26 
    14 CoreFoundation      0x3082cca7 __CFRunLoopDoSource1 + 166 
    15 CoreFoundation      0x3081f56d __CFRunLoopRun + 520 
    16 CoreFoundation      0x3081f277 CFRunLoopRunSpecific + 230 
    17 CoreFoundation      0x3081f17f CFRunLoopRunInMode + 58 
    18 GraphicsServices     0x31e445f3 GSEventRunModal + 114 
    19 GraphicsServices     0x31e4469f GSEventRun + 62 
    20 UIKit        0x31e51123 -[UIApplication _run] + 402 
    21 UIKit        0x31e4f12f UIApplicationMain + 670 
    22 Score Card       0x000029ef main + 70 
    23 Score Card       0x000029a4 start + 40 
) 
terminate called after throwing an instance of 'NSException' 
Program received signal: 「SIGABRT」. 
kill 
quit 

爲什麼會這樣撞我的應用程序?

+0

向我們展示'countTimer'的定義 – jer 2010-09-12 03:08:03

回答

0

它看起來像計時器從未初始化或它被釋放。有時像這樣的異常也會在隨機對象拋出時拋出。檢查你的指針並保持計數!

5

在一種預感,你做這樣的事情

countTimer = [NSTimer scheduledTimerWithTarget:...]; 

... 

if ([countTimer isValid]) 
{ 
    [countTimer invalidate]; 
} 

... 


if ([countTimer isValid]) 
{ 
    [countTimer invalidate]; 
} 

A「計劃的」計時器被添加到您的運行循環。然後由運行循環保留。使它失效將其從運行循環中刪除,因此它被釋放。如果沒有其他任何東西保留它,它是交易。當您嘗試再次使用它時,它會崩潰。

嘗試像[countTimer invalidate]; countTimer = nil;

或者,你可以保留的計時器,但要注意,計時器保留其目標,因此很容易與保留週期結束。

+0

Yah。由於您沒有保留定時器(因爲您通常不應該這樣做),所以您不知道它何時會被釋放,除非它將在調用回調方法之後。您應該在當時將其參考設置爲零,或者在您使其無效時,因爲它會在那之後自動釋放。 – 2010-09-12 03:38:22

+1

你知道它何時被運行循環*釋放*。你不知道什麼時候會被處理;那不一樣。 – 2010-09-12 13:02:49