I'm assuming you're using ARC. Remove this code:
{
NSOutputStream *outputStream;
NSInputStream *inputStream;
}
@synthesize inputStream, outputStream;
It's just confusing you between the properties and the instance variables. And the result of having it is that you're setting the instance variable directly and your stream instances are being deallocated by ARC.
Instead, once you've deleted that code above, access the streams with self.inputStream
and self.outputStream
. This will allow the properties to work properly and ensure that the 'retain' spec that you configure is respected.
Your original code might also just not be trying to set the properties... Your configuration should be:
NSInputStream *inputStream = (NSInputStream *) readStream;
NSOutputStream *outputStream = (NSOutputStream *) writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
self.inputStream = inputStream;
self.outputStream = outputStream;
Then change the property to:
@property (strong) NSInputStream *inputStream;
@property (strong) NSOutputStream *outputStream;