提前感谢任何帮助我的人。
我有一个简单的守护进程。我分配了一个课程,然后开始安排&重复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];
有人知道(g)上有什么(wr)吗?
答案 0 :(得分: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 climac [2833:903]计时器解雇了
2011-03-25 18:43:45.722 climac [2833:903]计时器解雇了
2011-03-25 18:43:48.722 climac [2833:903]计时器解雇了
2011-03-25 18:43:51.722 climac [2833:903]计时器解雇了