2011-12-31 65 views
0

我想要一個計時器類,可以發送消息到委託時有1/2/3秒去。iOS邏輯單元測試目標在單元測試通過NSTimer後崩潰

我的測試目標一直崩潰。

  • iOS邏輯單元測試目標。
  • 使用重複NSTimer測試持續時間的測試類
  • 一個沒有聲明的測試。測試通過,但隨後的目標與崩潰:

/Developer/Tools/RunPlatformUnitTests.include: line 415: 770 Bus error "${THIN_TEST_RIG}" "${OTHER_TEST_FLAGS}" "${TEST_BUNDLE_PATH}"

在我看來,它是某種內存分配錯誤的,但我想不出什麼我失蹤。該問題與停止計時器程序有某種關聯。只有當計時器耗盡時,目標纔會崩潰。

事情我已經試過

  • 建立與分析 - 沒有錯誤報告
  • 從連接標誌刪除-framework和UIKit
  • 刪除的dealloc - 這沒有任何影響

測試代碼

-(void)testGivenThreeSecondDurationAtOneSecondDelegateShouldBeToldToShowGreenCard { 
    JGTimerController *timer = [JGTimerController timerWithDurationValue:1 delegate:nil]; 
    [timer startTimer]; 

    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.1]]; 
} 

類代碼

@interface JGTimerController : NSObject { 
    NSNumber *duration; 
    NSTimer  *timer; 
    id <NSObject, JGTimerControllerDelegate> _delegate; 
} 
@property (nonatomic, retain) NSNumber *duration; 
... public methods... 
@end 


@implementation JGTimerController 

@synthesize duration; 

+(JGTimerController *)timerWithDurationValue:(NSUInteger)durationValue delegate:(id <JGTimerControllerDelegate>)delegate_ { 
    JGTimerController *instance = [[[JGTimerController alloc] initWithDurationValue:durationValue delegate:delegate_] autorelease]; 
    return instance; 
} 

-(JGTimerController *)initWithDurationValue:(NSUInteger)durationValue delegate:(id <JGTimerControllerDelegate>)delegate_ { 
    self = [super init]; 
    timer = nil; 
    [self setDurationValue:durationValue]; 
    _delegate = delegate_; 
    return self; 
} 

-(NSUInteger)durationValue { 
    NSNumber *result = [self duration]; 
    return result ? [result intValue] : 0; 
} 

-(void)setDurationValue:(NSUInteger)value_ { 
    [self setDuration:[NSNumber numberWithInt:value_]]; 
} 

-(BOOL)stopTimerAtZeroDuration:(NSTimer *)timer_ { 
    if ([self durationValue] == 0) { 
     [self stopTimer]; 
     return YES; 
    } 
    return NO; 
} 

-(void)startTimer { 
    if ([self stopTimerAtZeroDuration:nil]) 
     return; 

    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerDidCountDownByASecond:) userInfo:nil repeats:YES]; 
} 

-(void)stopTimer { 
    if ([self durationValue] == 0 && [_delegate conformsToProtocol:@protocol(JGTimerControllerDelegate)]) 
     [_delegate showRedCard]; 

    [timer invalidate]; 
    [timer release]; 
} 

-(BOOL)timerIsRunning { 
    return (timer != nil); 
} 

-(void)timerDidCountDownByASecond:(NSTimer *)timer_ { 
    [self setDurationValue:[self durationValue] - 1]; 
    [self stopTimerAtZeroDuration:timer_]; 
} 

-(void)dealloc { 
    [_delegate release]; 
    [timer release]; 
    [duration release]; 
    [super dealloc]; 
} 

@end 

回答

1

同樣,定時器不應該被釋放。 NSTimer與其他NSObject一樣,如果您沒有分配,複製或保留,則不需要釋放。

+0

我很肯定我試圖消除[定時發佈]線,它仍然墜毀。但刪除了這個,嘿presto!有效。非常感謝! – 2011-12-31 09:33:08

2

應該沒有[_delegate release]dealloc因爲你沒有保留。