直到今天,以下功能一直运行良好。今天,我在应用程序的“构建设置”中添加了arm64体系结构标志,并在应用程序的“其他链接器”标志(在“构建设置”中)中添加了-ObjC,现在对initWithFormat:的一个看似无害的调用使该应用程序崩溃了!

函数是这样的:

void CRLog(NSString* temp, ...)
{
    va_list arg_list;
    va_start(arg_list, temp);

    NSLogv(temp, arg_list);
    //The line that causes the crash
    NSString* str = [[[NSString alloc] initWithFormat:temp arguments:arg_list] autorelease];

    // do something here with str
    ...

    va_end(arg_list);
}


具体来说,导致应用程序崩溃的CRLog调用为:

CRLog(@"url:%@",para);


而para是

http://api.madserving.com/tc.m?aid=2&mod=iPhone%20Simulator&nt=3&os=1&osv=8.3&lng=en_US&jb=&apn=com.XYZ.ABC&av=1.8.0&aas=0&pv=1.1.2&ua=Mozilla/5.0%20(iPhone;%20CPU%20iPhone%20OS%208_3%20like%20Mac%20OS%20X)%20AppleWebKit/600.1.4%20(KHTML,%20like%20Gecko)%20Mobile/12F69


我的猜测是,参数中的%跳出了initWithFormat,但奇怪的是,这种方法只有在成功使用了许多个月之后才在今天发生。

有什么建议么?

最佳答案

您不能两次使用arg_list。 NSLogv将更改arg_list。您需要调用va_start / va_end两次。

您调用了未定义的行为,该行为随时可能发生。

关于ios - 不知道为什么initWithFormat的参数包含%时会导致崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30994853/

10-13 02:04