Question

In the following code NSError instantiates (match is GameCenter's GKMatch):

    ...
    NSString * deviceID = appDelegate.deviceID;
    MessageWithDeviceID message;
    message.message.messageType = kMessageTypeDeviceID;
    const char* const deviceIDAsUTF8 = deviceID.UTF8String;
    for (size_t idx=0;idx<32;idx++)
        message.deviceID[idx] = deviceIDAsUTF8[idx];

    NSData *data = [NSData dataWithBytes:&message length:sizeof(message)];
    NSError * error;
    [match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];

    data  = nil;
    if (error) 
    NSLog(@"An error is occured: %@",error);
    ...

but the app crashes at the last shown line with EXC_BAD_ACCESS or SIGABRT -[NSCFString userInfo (or domain, localizedDescription, code etc)] unrecognized selector sent to instance 0x.... So I can see that error is not nil and it is instantiated object but I can't get any information from it. I tried to get the following:

[error localizedDescription];
[error domain];
[error code];
[error description];
etc.

but app still crashes at NSLog line no matter which info I'm trying to get. I know the approach isn't right and I need to use BOOL variable in line which sends data but thats not the point of question. The question is whats wrong with the NSError object? I could see error's fields in debugger (those expandable info) like:

NSObject       NSObject
void *         _reserved
NSInteger      _code        875902004  Summary
NSString *     _domain
NSDictionary * _userInfo

So it seems like error object does have all the info but its unaccessible... Why is it so? Is NSError somehow not fully/completely instantiated or something?

Was it helpful?

Solution

Try initializing your error pointer to nil

NSError * error = nil;

You get a crash because there was no error and your pointer contains a random address.

OTHER TIPS

Replace this line.

NSError* error = nil;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top