ios - 在具有格式的字符串上崩溃 EXC_BAD_ACCESS

标签 ios nsstring ip signal-processing

我正在处理原始 IP 数据以处理视频信号 (ATSC-MH)。但是,我在一些基本问题上遇到了问题,我现在显然很生气,需要另一双眼睛。

这是翻转的功能。有趣的是它工作了一段时间,我不记得我改变了什么。 **d 行是错误日志中引用的行。我不能做太多 NSLog 调试,因为流来自附件(所以没有直接调试管道 :()。

-(NSString*)reportSMT{
//NSString* ret = @"Not implemented yet";
__autoreleasing NSMutableString* ret = [[NSMutableString alloc] initWithFormat:@"\nSMT:\n  SecSynInd:%@ PriInd:%@\n  SecLen:%d SMTMHProVer:%d\n  EnID:%d VerNum:%d\n  CurNxtInd:%@ SecNum:%d\n  lastSec#:%d #Servs:%d\n\n",(Header.section_syntax_indicator?@"YES":@"NO"),(Header.private_indicator?@"YES":@"NO"),Header.section_length,Header.SMT_MH_protocol_version,Header.ensemble_id,Header.version_number,(Header.current_next_indicator?@"YES":@"NO"), Header.section_number,Header.last_section_number,Header.num_MH_services];
[ret appendString:[NSString stringWithFormat:@"SMT Table:\n"]];
for (int i = 0; i<Header.num_MH_services; i++) {
    **[ret appendString:[NSString stringWithFormat:@"Serv(%d):\n  ServID:%d MultiEnServ:%d\n  ServStat:%d ServSPInd:%@\n  ServShotName:%@\n  ServCat:%d\n  source:%@ dest:%@\n  #MHServComps:%d\n",i,Services[i].MH_service_id,Services[i].multi_ensemble_service,Services[i].MH_service_status,(Services[i].SP_indicator?@"YES":@"NO"),[NSString stringWithUTF8String:(char*)Services[i].short_MH_service_name],(Services[i].service_source_IP_address_flag?[Utility ParseIP:Services[i].service_source_IP_address]:@"N/A"),(Services[i].service_destination_IP_address_flag?[Utility ParseIP:Services[i].service_destination_IP_address]:@"N/A"),Services[i].num_components]];**
    for (int m=0; m<Services[i].num_components; m++) {
        [ret appendString:[NSString stringWithFormat:@"  Comp(%d)(essential:%@):\n    port#count:%d compSource:%@\n    compDest:%@ destPort:%d\n",m,(Services[i].components[m].essential_component_indicator?@"YES":@"NO") ,Services[i].components[m].port_num_count,(Services[i].components[m].component_source_IP_address_flag?[Utility ParseIP:Services[i].components[m].component_source_IP_address]:@"N/A"),(Services[i].components[m].component_destination_IP_address_flag?[Utility ParseIP:Services[i].components[m].component_destination_IP_address]:@"N/A"),Services[i].components[m].component_destination_UDP_port_num]];
    }
}

return [ret copy];
}

这是实用程序 parseIP 函数。虽然它没有改变任何东西来评论对它的调用并在那里硬编码一个值:

+(NSString*)ParseIP:(long)ip{
__autoreleasing NSString* ret = nil;

if (ip) {
    unsigned char* ipPtr = (unsigned char*)&ip;
    unsigned char ipc[4];
    for (int i=0; i<4; i++) {
        ipc[i] = *(ipPtr+i);
    }
    ret = [NSString stringWithFormat:@"(%d.%d.%d.%d)",ipc[3],ipc[2],ipc[1],ipc[0]];
}

return ret;
}

这是 SMT 这部分的结构:

struct SMTChunk{
unsigned int MH_service_id;//16
unsigned char multi_ensemble_service;//2
unsigned char MH_service_status;//2
bool SP_indicator;//1
unsigned char short_MH_service_name_length;//3 /* m */ 
unsigned char* short_MH_service_name;//16*m
unsigned char reserved2;//2 should be 11
unsigned char MH_service_category;//6
unsigned char num_components;//5
bool IP_version_flag;//1
bool service_source_IP_address_flag;//1
bool service_destination_IP_address_flag;//1
unsigned long service_source_IP_address;//32 if (service_source_IP_address_flag)
unsigned long service_destination_IP_address;//32 if (service_destination _IP_address_flag)
struct SMTComponent* components;
unsigned char reserved4;//4 1111(f)
unsigned char num_MH_service_level_descriptors;//4
struct SMTServiceDescriptor* descriptors;
};

就像我之前说过的那样,所以我很确定填充数据结构的解析器没问题。

设备日志(重要部分):

Date/Time:       2012-03-06 00:56:40.480 -0600
OS Version:      iPhone OS 5.0.1 (9A405)
Report Version:  104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000a
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libobjc.A.dylib                 0x300e4fb6 objc_msgSend + 10
1   Foundation                      0x30dd9d14 _NSDescriptionWithLocaleFunc + 44
2   CoreFoundation                  0x335d699e __CFStringAppendFormatCore + 7998
3   CoreFoundation                  0x33551210 _CFStringCreateWithFormatAndArgumentsAux + 68
4   Foundation                      0x30dd9c3e +[NSString stringWithFormat:] + 54
5   APT-test                        0x000c9630 -[SMT reportSMT] (SMT.m:178)
6   APT-test                        0x000c54bc -[VideoViewController saveTimerFun:] (VideoViewController.mm:940)
7   Foundation                      0x30e79616 __NSFireTimer + 138

感觉就像我一直在开发这个应用程序,所以欢迎您提供任何帮助或指示。

提前致谢!

最佳答案

printf 通配符和参数似乎没有正确平衡:

[NSString stringWithFormat:@"1%d 2%d 3%d 4%d 5%@ 6%@ 7%d 8%@ 9%@ A%d",
    /* 1 */i,
    /* 2 */Services[i].MH_service_id,
    /* 3 */Services[i].multi_ensemble_service,
    /* 4 */Services[i].MH_service_status,
    /* 5 */(Services[i].SP_indicator?@"YES":@"NO"),
    /* 6 */[NSString stringWithUTF8String:(char*)Services[i].short_MH_service_name],
    /* 7 */(Services[i].service_source_IP_address_flag?[Utility ParseIP:Services[i].service_source_IP_address]:@"N/A"),
    /* 8 */(Services[i].service_destination_IP_address_flag?[Utility ParseIP:Services[i].service_destination_IP_address]:@"N/A"),
    /* 9 */Services[i].num_components]];

看起来您正在尝试将位置 9 的 %@ 说明符与 Services[i].num_components 匹配,这很可能导致 EXC_BAD_ACCESS。您最好重写代码以使其更具可读性,这种困惑只会带来麻烦。

关于ios - 在具有格式的字符串上崩溃 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9579499/

相关文章:

objective-c - NSString : number of a word

c# - 将 IP 转换为字节/转换回字符串

基于Python的IP地址计算器(IPCALC)代码

ios - iPhone X Swift 底部有空白

ios - 使用 JSQLlocationMediaItem 发送位置

ios - Swift 键盘不会被 touchesBegan 关闭

ios - Swift:按下按钮时编辑标签的值

objective-c - 在 iPhone 上使用 NSRegularExpression 提取 URL

objective-c - NSString 特殊字符编码

networking - 链接本地地址的用途是什么?