2011-05-31 60 views
0
-(BOOL)ChangeTimer:(unsigned short)wTimerIds withPeriod:(uint8_t)uPeriod 
{ 
stRs232Timer* pEvent; 
    NSLog(@"Into the changeTimer"); 
    NSLog(@"%d",wTimerIds); 
    pEvent = (stRs232Timer*)[[m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]]bytes]; 
    NSLog(@"bPersistent:%d",pEvent->bPersistent); 
    NSLog(@"wAppTimerId:%d",pEvent->wAppTimerId); 
    NSLog(@"uPeriod:%d",pEvent->uPeriod); 
    NSLog(@"bStopped:%d",pEvent->bStopped); 
    //NSLog(@"%@",pEvent); 
    if(pEvent!=nil){ 
     pEvent->bStopped = NO; 
     pEvent->uPeriod = uPeriod; 
     NSLog(@"completed"); 
    } 

    NSLog(@"The dict is:%@",m_cAppIdMap); 

    NSLog(@"jsagxs:%@",m_cAppIdMap); 
    return YES; 
} 

如果我刪除了上面代碼中的註釋,我得到一個錯誤EXC_BAD_ACCESS.Why是這樣。嘗試打印結構變量時出錯 - 目標C

+1

您是否已經設置了斷點並檢查了pEvent的值?我認爲,這將有助於... – cem 2011-05-31 11:40:11

回答

3

當您使用「%@」格式說明符時,您告訴運行時在關聯指針上調用-descriptionWithLocale:-description方法,該關聯指針的類暗示爲NSObject的子類。

結構不是Objective-C對象,因此沒有-descriptionWithLocale:-description方法。 這是什麼導致EXC_BAD_ACCESS

您應該使用%p來打印不指向Objective-C對象的指針。

+0

感謝您向我解釋有關錯誤原因。我沒有任何異常得到正確的輸出。 – spandana 2011-05-31 12:04:25

1

格式說明%@作品由期望的NSObject子類,並調用descriptionWithLocale:description(如果descriptionWithLocale不存在),以獲得NSString描述對象。 stRs232Timer只是一個C結構 - 不是Objective C對象。正如你所看到的,當運行時試圖把它當作一個對象爆炸時。

嘗試使用%p說明符,它將只打印該項目的地址(即帶有0x前綴的指針的值)。

+0

感謝您解釋異常的原因。我按照說的去做了輸出。 – spandana 2011-05-31 12:05:29