I have a callback method when a socket receives data like this...
+ (void) dataReceived:(NSData *)data {
NSData *dataCopy = [data copy];
NSString *msg = [[NSString alloc] initWithData:dataCopy encoding:NSUTF8StringEncoding];
NSLog(@"msg: %@", msg);
}
I can see in the debugger that both data
and dataCopy
contain the correct string data. The dataCopy
is made without an issue but when the msg
line is called, I blow up w/ this.
2015-04-02 15:45:10.179 Sandbox[15218:539849] -[__NSCFString bytes]: unrecognized selector sent to instance 0x7f9c71447870 2015-04-02 15:45:10.184 Sandbox[15218:539849] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x7f9c71447870'
I don't understand, I can see the data is correct, I thought making the copy is what prevents access to overwritten memory. Any idea what is going on here?
Seems that your "delegate" method gets called with a NSString instead of a NSData. Copying the received object won't help you in this case, as you're just copying a string into another string. You'll need to dig deeper and see why dataReceived: doesn't receive the expected NSData object.