gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:55:54 27 4
gpt4 key购买 nike

我正在开发一个 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;

    -(无效)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 *) 数据{ 国际长度; uint32_t 长度 = (uint32_t)htonl([数据长度]); //[oStream write:(const uint8_t *) "/n"maxLength:2]; //发送截图图像的长度,然后发送图像 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{ 开关(事件代码){ 案例 NSStreamEventHasSpaceAvailable: 如果(流 == 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];
    }
    });

    }
    }

只有我遇到过这种问题吗?我使用错误的方法吗?有什么解决问题的建议吗?

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

最佳答案

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

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

关于iOS应用程序分配越来越多的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22658135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com