2011-05-11 115 views
0

所以我有一個應用程序使用NSTimer。問題是,當NSTimer運行時,我的應用程序將崩潰EXC_BAD_ACCESS。我只是開始使用objective-c,所以我不知道如何正確調試它。如果我認爲請撥打-(void)logIn{}[self logIn];,該應用程序將起作用。 我的代碼:
.HNSTimer崩潰我的應用程序

@interface DJ_WAppDelegate : NSObject { 
    NSTimer *loginTimer; 
    } 

    -(void)logIn; 

    @end 

.M

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    [self logIn]; // this works 
    loginTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(logIn) userInfo:nil repeats:YES]; // this fails 
} 

- (void)logIn { 
    NSLog(@"Logging in..."); 

    // if I comment out these 2 lines it works with the NSTimer!?... (I've would have more code below) 
    NSURL *loginConn = [NSURL URLWithString:[NSString stringWithFormat:@"some-website.com"]]; 
    NSInteger loginReturn = [[NSString stringWithContentsOfURL:loginConn encoding:NSASCIIStringEncoding error:nil] intValue]; 

    // when "loginReturn" return "OK" the timer (loginTimer) will be stopped with: [loginTimer invalidate]; 

    // more code would be below... (which works!) 
} 


所以這個問題是與NSURL它認爲。 感謝您的幫助。

編輯1: 這裏的崩潰堆棧:

EException Type: EXC_BAD_ACCESS (SIGBUS) 
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000020 
Crashed Thread: 0 Dispatch queue: com.apple.main-thread 

Application Specific Information: 
objc_msgSend() selector name: respondsToSelector: 


Thread 0 Crashed: Dispatch queue: com.apple.main-thread 
0 libobjc.A.dylib     0x9603bed7 objc_msgSend + 23 
1 com.apple.CoreFoundation  0x922ed5f2 _CFStringAppendFormatAndArgumentsAux + 3138 
2 com.apple.CoreFoundation  0x922ec979 _CFStringCreateWithFormatAndArgumentsAux + 105 
3 com.apple.Foundation   0x9656ebfb -[NSPlaceholderString initWithFormat:locale:arguments:] + 163 
4 com.apple.Foundation   0x9656eaae +[NSString stringWithFormat:] + 88 
5 com.who.DJ-W    0x00001d56 -[DJ_WAppDelegate logIn] + 116 (DJ_WAppDelegate.m:108) 
6 com.apple.Foundation   0x965b08d4 __NSFireTimer + 141 
7 com.apple.CoreFoundation  0x922ffadb __CFRunLoopRun + 8059 
8 com.apple.CoreFoundation  0x922fd464 CFRunLoopRunSpecific + 452 
9 com.apple.CoreFoundation  0x922fd291 CFRunLoopRunInMode + 97 
10 com.apple.HIToolbox    0x91646e04 RunCurrentEventLoopInMode + 392 
11 com.apple.HIToolbox    0x91646bb9 ReceiveNextEventCommon + 354 
12 com.apple.HIToolbox    0x91646a3e BlockUntilNextEventMatchingListInMode + 81 
13 com.apple.AppKit    0x9265378d _DPSNextEvent + 847 
14 com.apple.AppKit    0x92652fce -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 156 
15 com.apple.AppKit    0x92615247 -[NSApplication run] + 821 
16 com.apple.AppKit    0x9260d2d9 NSApplicationMain + 574 
17 com.who.DJ-W    0x00001c92 start + 54 

希望這有助於...查找錯誤

編輯2:
隨着殭屍模式上,我得到這個:*** -[CFString respondsToSelector:]: message sent to deallocated instance 0x46d550我希望這有助於。

編輯3:
Ball:這裏是原來的URL(帶走這裏的域)/DJW/work.php?user=iBlackbirdi&udid=00000000-0000-1000-80005&key=660e5744e&cmd=slocau&type=get

+1

如果您發佈崩潰堆棧,找出崩潰的情況會更容易。 – smorgan

+0

定時器何時應該停止射擊?請爲您的活動可執行文件[請啓用NSZombie](http://cocoa-nut.de/?p=16)並查看控制檯輸出是否產生新的內容。 –

+0

我不知道如果你的原始字符串的URL包括一些網址編碼%escapes這會混淆stringWithFormat – kball

回答

3

這裏有幾個問題:

  1. djwLog是一個類的方法,所以你應該把它的類而不是實例 這樣的:

  2. URLFromString:方法需要字符串包含由RFC 1808指定的有效URL。沿線的東西[NSURL URLFromString:@"http://example.com/foo"]

  3. stringWithContentsOfURL:url…是一種同步方法。除非你有這個代碼運行在一個單獨的線程中,否則不應該使用它。查看NSURLConnection以便從一個URL異步加載數據。對此使用同步調用是一個不好的想法。總是。

  4. intValue返回signed integer。要獲得NSInteger,請使用integerValue。此外,如果您使用stringWithContentsOfURL,請確保在致電 integerValue之前檢查結果是否爲nil,否則如果URL調用失敗或未返回數據,則可能得到0的結果。在任何情況下,您所調用的API的真正解析器都是一個好主意。

+0

很多東西我不知道...謝謝你的回覆。它的目的是,如果URL調用失敗,結果爲0!因爲服務器只會返回數字......無論如何,這解決了我的問題。 – Silicone

2

你調用一個類的方法,如果它是一個實例方法:

[self djwLog:@"Logging in..."]; 

由於沒有名爲djwLog的實例方法,因此應用程序崩潰。你應該直接調用的NSLog,使它成爲一個實例方法 - 或最好做一個宏記錄:

#ifdef DEBUG 
# define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__]) 
#else 
# define DLog(...) do { } while (0) 
#endif 

如果不是的NSLog寫DLOG - 當DEBUG標誌已經定義它只會被記錄。除此之外,它還會記錄日誌消息的來源 - 因此您可以看到日誌條目來自哪個類/方法。

+0

感謝您的答覆。我知道我可以直接用'NSLog()'來完成。在我寫這篇文章後,我注意到我發佈的代碼並不像我這樣做。 '[self djwLog @「」]'確實有效。我將在我的問題中用'NSLog()'替換我的方法。 – Silicone