I'm developing an iOS application which is using socket connection to share it's screen with another device. It's working well and it's sending the screenshots but the memory which the application is allocation keeps on growing and growing until it receives memory warning after several seconds and then it starts sending wrong data to the receiving server so that the server crashes. Here is the code I use right now:
The method in my main ViewController.m:
-(void) activateShareScreen { newClient *nc = [[newClient alloc] init]; [nc connectToServerUsingCFStream]; dispatch_queue_t backgroundQueue = dispatch_queue_create("No", 0); dispatch_async(backgroundQueue, ^{ while (true){ @autoreleasepool { UIGraphicsBeginImageContext(mapView_.frame.size); [mapView_.layer renderInContext:UIGraphicsGetCurrentContext()]; bitmap = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } [nc writeToServer:UIImageJPEGRepresentation(bitmap, 50)]; } }); NSLog(@"Share screen button tapped");
}
And it is calling this file:
#import "newClient.h"
@implementation newClient {
NSInputStream *iStream; NSOutputStream *oStream; CFReadStreamRef readStream; CFWriteStreamRef writeStream; BOOL boolean;
}
-(void) connectToServerUsingCFStream{
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef) @"192.168.1.9", 8080, &readStream, &writeStream); if (readStream && writeStream) { //CFReadStreamSetProperty(readStream, // kCFStreamPropertyShouldCloseNativeSocket, // kCFBooleanTrue); //CFWriteStreamSetProperty(writeStream, // kCFStreamPropertyShouldCloseNativeSocket, // kCFBooleanTrue); iStream = (__bridge_transfer NSInputStream *)readStream; oStream = (__bridge_transfer NSOutputStream *)writeStream; // [iStream setDelegate:self]; //[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] // forMode:NSDefaultRunLoopMode]; [iStream open]; // [oStream setDelegate:self]; //[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] // forMode:NSDefaultRunLoopMode]; [oStream open]; // data = UIImageJPEGRepresentation(bitmap, 50); boolean = YES;
- (void) writeToServer:(NSData *) data{ int len; uint32_t length = (uint32_t)htonl([data length]); //[oStream write:(const uint8_t *) "/n" maxLength:2]; // Sending out the length of the screenshot image and then the image len=[oStream write:(const uint8_t *)&length maxLength:4]; NSLog(@"len=%d",len); //[oStream write:(const uint8_t *) "/n" maxLength:2]; len=[oStream write:(const uint8_t *)[data bytes] maxLength:[data length]]; NSLog(@"len=%d",len); //[oStream write:(const uint8_t *)"/n" maxLength:0]; }
/*- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { switch(eventCode) { case NSStreamEventHasSpaceAvailable: if(stream == oStream) { dispatch_async(dispatch_get_main_queue(), ^{ @autoreleasepool {
int len; uint32_t length = (uint32_t)htonl([data length]); // Sending out the length of the screenshot image and then the image len=[oStream write:(const uint8_t *)&length maxLength:4]; NSLog(@"len=%d",len); if (len<0) { [oStream write:(const uint8_t *)[data bytes] maxLength:length]; } [oStream write:(const uint8_t *)"/n" maxLength:0]; } }); } }
Am I the only one who has experienced that kind of problem? Am I using wrong methods? Any suggestions how to solve the problem?
Here's a screenshot of Instruments when I run the app in it: