• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何修复 Objective-C 中的内存泄漏?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:42:39 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我构建了一个从 HockeyApp 获取报告的简单应用程序。但是,当我使用内存泄漏工具运行应用程序时,它显示当我执行 getReport 操作时存在内存泄漏。我无法理解仪器中显示的所有信息。

这是导致内存泄漏的按钮操作方法:

- (IBAction)getReportsid)sender {

//initialize url that is going to be fetched.
NSURL *url = [NSURL URLWithString"https://rink.hockeyapp.net/api/2/apps/APP_ID/crash_reasons"];

//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:tokenReceived forHTTPHeaderField"X-HockeyAppToken"];

[request setHTTPMethod"GET"];
[request setValue"application/x-www-form-urlencoded" forHTTPHeaderField"Content-Type"];

//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.getReportConnection = connection;

}


- (void)connectionNSURLConnection *)connection didReceiveDataNSData*)data{

    if (connection==getReportConnection) {

     [self.receivedData appendData:data];

     NSLog(@"data is %@",data);

    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSError *e = nil;
    NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
    NSLog(@"login json is %@",JSON);
     NSLog(@"reason json is %@",JSON[@"reason"]);

    [JSON[@"crash_reasons"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {


        [reportArray addObjectbj[@"reason"]];
         NSLog(@"index = %lu, Object For title Key = %@", (unsigned long)idx, obj[@"reason"]);
    }];

    NSError *error = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData
                                                         options:kNilOptions error:&error];

    if (error != nil) {
        NSLog(@"Error parsing JSON.");
    }
    else {
        NSLog(@"Array: %@,array count is %d", jsonArray,jsonArray.count);
    }

   // [reportArray addObject:[jsonArray objectAtIndex:0]];

    if (JSON!=NULL) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle"Reports succesfully retrieved" message"" delegate:self cancelButtonTitle"Ok" otherButtonTitles: nil];
        [alert show];
    }

       }
}

 // This method receives the error report in case of connection is not made to server.
 - (void)connectionNSURLConnection *)connection didFailWithErrorNSError *)error{

UIAlertView *errorAlert=[[UIAlertView alloc]initWithTitle"Wrong Login" message:nil delegate:self cancelButtonTitle"ok" otherButtonTitles: nil];
[errorAlert show];
NSLog(@"error is %@",error);
}

// This method is used to process the data after connection has made successfully.
- (void)connectionDidFinishLoadingNSURLConnection *)connection{

}

我发现内存泄漏发生在 didRecieveData 方法中出现警报 View 之前。

这里是内存泄漏工具显示内存泄漏的截图:

enter image description here

我不明白代码的哪一部分导致了内存泄漏。谁能告诉我如何使用泄漏工具识别导致内存泄漏的代码部分?

编辑:当我在模拟器上运行应用程序时,仪器没有显示任何内存泄漏:

截图如下: enter image description here

当我在设备上运行应用程序时,仪器再次显示内存泄漏: enter image description here

我查看了泄漏部分,发现 NSmutableArray 导致了泄漏: enter image description here enter image description here

我在我的代码中只使用了一个 NSMutableArray。我在 .h 文件中声明了它:

@property (nonatomic,strong) NSMutableArray *reportArray;

并在viewDidLoad中分配:

reportArray=[[NSMutableArray alloc]init];

并将其加载到 didRecieveData:

 [reportArray addObjectbj[@"reason"]];

堆栈跟踪快照:

enter image description here enter image description here enter image description here



Best Answer-推荐答案


试试这个:

reportArray = [[[NSMutableArray alloc] init] autorelease];

在您的 connectionDidFinishLoading:connection:didFailWithError: 方法中设置

reportArray = nil

最后在 Project > Build Phases > Compile Sources 添加 -fno-objc-arc 作为编译器标志此文件 (已编辑,抱歉)。然后再次点击 Product menu >Analyze (command + shift + B),查看是否还存在内存泄漏。

关于ios - 如何修复 Objective-C 中的内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31208743/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap