iOS应用分配越来越多的内存

时间:2014-03-26 10:29:36

标签: ios

我正在开发一个iOS应用程序,它使用套接字连接与其他设备共享它的屏幕。它运行良好并且它正在发送屏幕截图,但是应用程序分配的内存不断增长和增长,直到几秒钟后它收到内存警告,然后它开始向接收服务器发送错误的数据,以便服务器崩溃。这是我现在使用的代码:

  • 我的主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");
    

    }

  • 它正在调用此文件:

    #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]; } }); } }

我是唯一经历过这种问题的人吗?我使用错误的方法吗?有什么建议如何解决问题?

这是我在其中运行应用程序时的工具屏幕截图: Screenshot from instruments tool

1 个答案:

答案 0 :(得分:2)

@autoreleasepool {
    while (true){
       ...
    }
}

自动释放永远不会完成,它应该放在INS循环中