2014-02-12 184 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

我改变了它,但错误不是固定的。仍然崩溃。 –