Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

va_list crash on 64-bits simulator

When using 64bits iOS simulator the init function below crashes with EXC_BAD_ACCESS (code=1) error. Would anyone know why ? And how to fix it properly.

For information: 'format' is not nil, and it works just fine on 32bits simulator and any 32/64 bits iPhone/iPad devices.

void Log (NSString * format, ...)
{
   va_list argList;
   va_start(argList, format);
   NSLogv(format, argList);
   NSString* string = [[NSString alloc] initWithFormat: format arguments: argList];
   va_end(argList);

...
}

called first thing in AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   Log(@"app options %@", launchOptions);
   ...
}
like image 850
kokluch Avatar asked May 23 '14 15:05

kokluch


2 Answers

Somehow A-Live comment gave me an idea and I found out how to avoid the crash.

I was using argList twice in the same va_start/va_end block

[[NSString alloc] initWithFormat: format arguments: argList];

and

NSLogv(format, argList);

It seems that iOS 64-bits simulator don't like it. Don't know why it works just find in any other plateforme (and real devices too). So I fixed it by making two deferent bock like that

va_list argList;

va_start(argList, format);
NSString* string = [[NSString alloc] initWithFormat: format arguments: argList];
va_end(argList);

va_start(argList, format);
NSLogv(format, argList);
va_end(argList);

Hope it will help someone. If someone knows why, I am still curious to heard about it.

like image 182
kokluch Avatar answered Sep 23 '22 17:09

kokluch


I also just ran into this problem last night. I've filed a Radar for this.

My work around right now is making a va_copy:

    NSString *description = nil;
    va_list vaListCopy;

    va_copy(vaListCopy, arguments);

    if (format) {
        description = [[NSString alloc] initWithFormat:format arguments:vaListCopy];
    }

and then later on I call the next method

    [[BKAssertRecordHandler currentHandler] recordFailureWithCondition:condition function:function file:file line:line description:format arguments:arguments backtrace:backtrace];

In other words, I use the copy locally. I've done some experimentation. I made a copy if the va_list and then compared that to the original, after the original was used like so.

    description = [[NSString alloc] initWithFormat:format arguments:arguments];

Output during the byte comparison: Mismatch at index 8 (orig=0xd8 copy=0xe0). Prior to the call, I also did the byte compare, and they were the same.

So for whatever reason on the 64-bit simulator, initWithFormat:arguments and NSLogv corrupt the va_list.

like image 22
Mobile Ben Avatar answered Sep 25 '22 17:09

Mobile Ben