2014-02-12 137 views
0

基本上,我需要幫助解決堆棧溢出錯誤我一直在我的ipad/iphone應用程序。我一直在試圖修復它在過去的10天,但沒有用。沒有遞歸,因爲堆棧溢出錯誤,無遞歸

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 

無論何時在UITextField中更改字符/字符串時都會被調用一次。

基本上這裏就是我有一個問題的一部分:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if(textField == m_curValueField) 
    { 
     if([string rangeOfCharacterFromSet:[[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFabcdef"] invertedSet]].location != NSNotFound) 
     { 
      return NO; 
     } 

     NSString *addedString = m_curValueField.text; 
     addedString = [addedString stringByReplacingCharactersInRange:range withString:string]; 
     if(m_instructionType == TYPE_1) 
     { 
      if(addedString.length > 4) 
       return NO; 
     } 
     else if(m_instructionType == TYPE_2) 
     { 
      if(addedString.length > 8) 
       return NO; 
     } 

     NSString *result = @"UNDEFINED"; 

     if([m_curValueField.text isEqualToString:addedString]) 
     { 
      return NO; 
     } 
     if(m_instructionType == TYPE_THUMB) 
     { 
      if(addedString.length == 4) 
      { 
       NSString *temp = @""; 
       temp = [temp stringByAppendingString:m_curValueField.text]; 
       temp = [temp stringByAppendingString:string]; 

       NSString *firstByte = [temp substringWithRange:NSMakeRange(0, 2)]; 
       NSString *secondByte = [temp substringWithRange:NSMakeRange(2, 2)]; 
       temp = [NSString stringWithFormat:@"%@%@",secondByte,firstByte]; 

       uint16_t bytes; 
       memcpy(&bytes, [temp cStringUsingEncoding:NSASCIIStringEncoding], sizeof(uint16_t)); 


       char bits [16]; 
       sprintf (bits,BYTETOBINARYPATTERN,BYTETOBINARY(bytes)); 
       result = [m_thumbconverter HexToThumb:bits]; 

      } 
     } 
     else if(m_instructionType == TYPE_2) 
     { 
      if(addedString.length == 8) 
      { 
       NSString *temp = @""; 
       temp = [temp stringByAppendingString:m_curValueField.text]; 
       temp = [temp stringByAppendingString:string]; 

       NSString *firstByte = [temp substringWithRange:NSMakeRange(0, 2)]; 
       NSString *secondByte = [temp substringWithRange:NSMakeRange(2, 2)]; 
       NSString *thirdByte = [temp substringWithRange:NSMakeRange(4, 2)]; 
       NSString *fourthByte = [temp substringWithRange:NSMakeRange(6, 2)]; 
       temp = [NSString stringWithFormat:@"%@%@%@%@",fourthByte,thirdByte,secondByte,firstByte]; 

       uint32_t bytes; 
       memcpy(&bytes, [temp cStringUsingEncoding:NSASCIIStringEncoding], sizeof(uint32_t)); 

       char bits[32]; 
       sprintf (bits,BYTETOBINARYPATTERN,BYTETOBINARY(bytes)); 
       result = [m_armconverter HexToARM:bits]; 
      } 
     } 
     m_curNewValueField.text = [result copy]; 
    } 

    return YES; 
} 

它基本上轉換十六進制的ARM/Thumb。 HexToThumb/HexToARM函數可以工作,並返回一個值。基本上問題是,當m_instructionType等於TYPE_1時,應用程序崩潰,堆棧溢出。但是,當它等於TYPE_2時,新字段的值會相應更改。當我NSLog代碼在不同的位置,看起來代碼運行良好,HexToThumb函數返回一個正確的值。它甚至繼續,並在m_curNewValueField = [結果副本]後的日誌;實際上顯示在系統日誌中。 順便說一句,這裏是BYTETOBITPATTERN和BYTETOBINARY的定義:

#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d" 
#define BYTETOBINARY(bytes) \ 
(bytes & 0x80000000 ? 1 : 0), \ 
(bytes & 0x40000000 ? 1 : 0), \ 
(bytes & 0x20000000 ? 1 : 0), \ 
(bytes & 0x10000000 ? 1 : 0), \ 
(bytes & 0x8000000 ? 1 : 0), \ 
(bytes & 0x4000000 ? 1 : 0), \ 
(bytes & 0x2000000 ? 1 : 0), \ 
(bytes & 0x1000000 ? 1 : 0), \ 
(bytes & 0x800000 ? 1 : 0), \ 
(bytes & 0x400000 ? 1 : 0), \ 
(bytes & 0x200000 ? 1 : 0), \ 
(bytes & 0x100000 ? 1 : 0), \ 
(bytes & 0x80000 ? 1 : 0), \ 
(bytes & 0x40000 ? 1 : 0), \ 
(bytes & 0x20000 ? 1 : 0), \ 
(bytes & 0x10000 ? 1 : 0), \ 
(bytes & 0x8000 ? 1 : 0), \ 
(bytes & 0x4000 ? 1 : 0), \ 
(bytes & 0x2000 ? 1 : 0), \ 
(bytes & 0x1000 ? 1 : 0), \ 
(bytes & 0x800 ? 1 : 0), \ 
(bytes & 0x400 ? 1 : 0), \ 
(bytes & 0x200 ? 1 : 0), \ 
(bytes & 0x100 ? 1 : 0), \ 
(bytes & 0x80 ? 1 : 0), \ 
(bytes & 0x40 ? 1 : 0), \ 
(bytes & 0x20 ? 1 : 0), \ 
(bytes & 0x10 ? 1 : 0), \ 
(bytes & 0x08 ? 1 : 0), \ 
(bytes & 0x04 ? 1 : 0), \ 
(bytes & 0x02 ? 1 : 0), \ 
(bytes & 0x01 ? 1 : 0) 

非常感謝你。

+0

任何人都可以幫我嗎? –

回答

1

我會嘗試使用snprintf用適當的限制(32?)過sprintf

+0

會嘗試並通知您結果。但我不認爲這是原因。因爲我說過,函數實際上會返回一個nsstring,函數會使用發送給它的位中的每一位。 –

+0

非常感謝。這個修好了:) –

+1

太好了。我現在可能不需要告訴你這個:),但對於其他聽衆,IMO通常更喜歡'snprintf',以防萬一。 –

0

char bits [n];定義未能採取終止零字節sprintf會產生進去,應該是17和33,而不是16和32

+0

,但它對char位[32]工作正常。並且該函數返回一個正確的nsstring –

+0

我改變了它,但錯誤不是固定的。仍然崩潰。 –