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)で何が起こっているのか考えていますか?

4

1 に答える 1

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] タイマー起動

于 2011-03-26T01:48:02.330 に答える