2011-03-26 2 views
0

저를 도와 준 사람에게 미리 감사드립니다.CFRunLoopRun() 및 NSTimer -> 세분화 오류

나는 간단한 데몬을 가졌다. 나는 클래스를 할당하고 NSTimer 반복 예약 &을 시작 : 내 데몬이 살아있을 수 있도록

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(usage3GviaSysctl) userInfo:nil repeats:YES]; 

다음 내가 CFRunLoopRun()를 호출합니다.
int main(int argc, char *argv[]) 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    signal(SIGTERM, (sig_t)SIGTERM_handler); 

    helper = [[NMDaemonHelper alloc] init]; 
    [helper startNotificationServer]; 
    CFRunLoopRun(); 

    NSLog(@"NMDAEMON: will exit"); 
    [pool release]; 
    return 0; 
} 

이제 문제는 타이머 화재 후 나는 세그먼트 폴트를 얻을 수 있다는 것입니다. BT : 중 하나가 작동하지 않았다 타이머를 시작하는

objc_msgSend 
__NSFireTimer 
__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ 
__CFRunLoopDoTImer 
__CFRunLoopRun 
CFRunLoopRunSpecific 

다른 방법. 예 :

NSTimer *timeUpdateTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1 target:self selector:@selector(usage3GviaSysctl) userInfo:nil repeats:YES]; 
[[NSRunLoop currentRunLoop] addTimer:timeUpdateTimer forMode:NSDefaultRunLoopMode]; 

아무도 아이디어가 있습니까 (wr)? (g)?

답변

0

내 생각 엔 선택기에 올바른 형식이 없다 ... NSTimer 인수가 있어야하므로 선택기에 ":"가 있어야하므로 @selector (usage3GviaSysctl :)가됩니다.

은 ... 내가 직접 시도에 여기 이렇게 일이 뭔가 다른했다 경우에 내 코드를 것 같다 :

#import <Foundation/Foundation.h> 

@interface NMDaemonHelper : NSObject { 
    NSTimer *_aTimer; 
} 
- (void)startNotificationServer; 
@end 

@implementation NMDaemonHelper 

- (void)dealloc { 
    [_aTimer invalidate]; 
    [super dealloc]; 
} 

- (void)startNotificationServer { 
    _aTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(usage3GviaSysctl:) userInfo:nil repeats:YES]; 
} 

- (void)usage3GviaSysctl:(NSTimer *)aTimer { 
    NSLog(@"timer fired"); 
} 

@end 

void SIGTERM_handler(int signum) { 
    NSLog(@"SIGTERM_handler"); 
} 

int main(int argc, char *argv[]) { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    signal(SIGTERM, (sig_t)SIGTERM_handler); 

    NMDaemonHelper *helper = [[NMDaemonHelper alloc] init]; 
    [helper startNotificationServer]; 
    CFRunLoopRun(); 

    [helper release]; 
    NSLog(@"NMDAEMON: will exit"); 
    [pool release]; 
    return 0; 
} 

... 출력은 다음과 같습니다

pho0의 $의 ./climac
2011-03-25 18 : 43 : 36.723 climac [2833 : 903] 타이머 해고
2011-03-25 18 : 43 : 39.723 climac [2833 : 903] 타이머 해고
2011-03-25 18:43 : 42.722 클림 [2833 : 903] 타이머 해고
2011-03-25 18 : 43 : 45.722 climac 43 : 48.722 climac [2833 : 903] : 43 : 51.722 climac [2833 : 903] 타이머

소성 타이머 2011-03-25 18
소성 [2833 : 903] 타이머 2011-03-25 18
발사
+0

참고 : 'dealloc'은 호출되지 않습니다. _ [Using Timers : Memory Management] (http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Timers/Articles/usingTimers)를 참조하십시오. .html # // apple_ref/doc/uid/20000807-SW6) _. – danyowdee

+0

도와 주셔서 감사합니다. 사실, NSTimer로 인해 발생하지는 않지만 타이머의 action 메소드에서 sysctl을 사용했기 때문에 발생하는 것으로 나타났습니다. 이것은 크래시 기자가 잘못된 bt를 만들 것이기 ​​때문에 식별 할 수있는 것이 아니 었습니다. ( – YllierDev

+0

@danyowdee, dealloc이 이것을 완벽하게 구현했습니다. 이것은 고의적 인 예제이지만 실제 클래스를 사용한다면 – pho0