I wrote a BinaryReader class in Objective-C that can extract an NSString from bytes created using C#’s BinaryWriter.Write(string). It seems to work great, but when I call:
BinaryReader *binaryReader = [[BinaryReader alloc] initWithData:dataArray];
NSString *strName = [binaryReader readString];
// strName is not nil, but the value is no longer correct (seems to have been erased)
I stepped into the readString method, and everything looks great (the string is correct) up until the method returns (I think ARC is kicking in).
Why would this be happening?
Here’s my readString method:
-(NSString *)readString
{
NSUInteger bytesCount = (NSUInteger)[self read7BitEncodedInt];
const void *byteData = [_data bytes] + _dataIndex;
NSString *returnValue =
[[NSString alloc] initWithBytes:byteData length:bytesCount encoding:NSUTF8StringEncoding];
_dataIndex += bytesCount;
return returnValue;
}
The returnValue contains the proper string right before the method returns.
By request, here’s my init function for BinaryReader.
-(id)initWithData:(NSData *)theData
{
self=[super init];
if(self != nil)
{
_dataIndex = 0;
_data = theData;
}
return self;
}
and the relevant part of header
@interface BinaryReader : NSObject
{
@private
NSData *_data;
int _dataIndex;
}
Are you sure the result is actually broken? The code you posted seems fine and there should be no ownership/memory issues when compiled with ARC.
Lately there have been issues reported with lldb (the debugger) not showing correct values in some cases. Try to log the result using
NSLog.