CFRunLoopRun() 和 NSTimer ->分段错误

发布于 2024-10-27 16:41:50 字数 1132 浏览 0 评论 0原文

预先感谢任何帮助我的人。

我有一个简单的守护进程。我分配一个班级,然后开始预定的课程。重复 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)on(g) 发生了什么吗?

Thanks in advance to anyone who is helping me.

I've a simple daemon. I allocate a class and then start a scheduled & repeating NSTimer:

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

then I call CFRunLoopRun() so that my daemon will stay alive.

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;
}

Now the problem is that after the timer fires I get a segfault.
bt:

objc_msgSend
__NSFireTimer
__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__
__CFRunLoopDoTImer
__CFRunLoopRun
CFRunLoopRunSpecific

other ways to start the timer didn't work either. for example:

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

Has anybody an idea what's going (wr)on(g)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-11-03 16:41:50

我的猜测是你的选择器没有正确的格式......需要有一个 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] 计时器启动

My guess is your selector doesn't have the right format ... needs to have an NSTimer argument, so your selector has to have the ":" in it, so @selector(usage3GviaSysctl:).

... I tried it myself and seems to works so here's my code in case it was something else:

#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;
}

... and output is:

pho0$ ./climac
2011-03-25 18:43:36.723 climac[2833:903] timer fired
2011-03-25 18:43:39.723 climac[2833:903] timer fired
2011-03-25 18:43:42.722 climac[2833:903] timer fired
2011-03-25 18:43:45.722 climac[2833:903] timer fired
2011-03-25 18:43:48.722 climac[2833:903] timer fired
2011-03-25 18:43:51.722 climac[2833:903] timer fired

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文