• Incorrect decrement of the reference count of an object that is not owned at this point by the caller1


    第一种情况

    这种问题一般就是变量申请了内存并初始化了,但没有使用此变量,接着将此变量又重新赋值。如下:

    NSString *imageString = [[NSString alloc] init];  

    imageString = @"HResout";  

     第二种情况

    测出的问题提示是 Incorrect decrement of the reference count of an object that is not owned at this point by the caller

    问题出现在这一行[self.tableView initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    本人的这个类是继承UITableViewController的,所以它应该会有个成员是tableView的,我想初始化它风格的样式,但是这里出现了这个问题,原因应该是没有创建就初始化了,后来改成这个:

    self.tableView =[[[UITableView alloc ]initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped] autorelease]; 

    第三种情况

        LoginViewController *loginViewController = [[LoginViewController alloc] initwithLoginUrl: loginUrl];

        CustomNavigationController *customNavigationController = [[CustomNavigationController alloc]initWithRootViewController: loginViewController];

        customNavigationController.navigationBar.tintColor = NavgaitonBar_Color;

        [self.navigationController presentModalViewController: customNavigationController animated: YES];

        [loginViewController release];

        [customNavigationController release];

    红色为提示内存泄露的地方 

    只要把    LoginViewController *loginViewController = [[LoginViewController alloc] initwithLoginUrl: loginUrl];

    修改为     LoginViewController *loginViewController = [[LoginViewController alloc] initWithLoginUrl: loginUrl];

    就可以解决内存泄露(就一大小写的差别)

     

    在dealloc方法中使用[self.xxx release]和[xxx release]的区别?

    用Xcode的Analyze分析我的Project,会列出一堆如下的提示:
    Incorrect decrement of the reference count of an object that is not owned at this point by the caller

    仔细看了下代码,都是在dealloc方法中使用了[self.xxx release]这样的语句引起的,把代码改成了[xxx release]就没有问题了

    但我不明白究竟原因为何?我的理解是:不管@property xxx这里设定了是retain, copy还是assign,影响的总是setter方法,getter方法不会边,就是简单地return xxx完事了。而self.xxx就是调用[self getXxx]一样的,那么为什么这样的代码会引起Xcode Analyze的警告呢?

    比如这里http://blog.csdn.net/diyagoanyhacker/article/details/6996208
    他是因为初始化方法拼写错误导致的。

     

    self.xxx调用的是getter,而getter并非想当然的是 - (id) xxx{ return _xxx; }.
    有可能是  - (id) xxx{  return [[xxx retain] autorelease]; }还有各种情况。这时候你发release就不只是你想的_xxx接收的了。 

    所以,在dealloc里面可以这样释放:
    self.xxx = nil;
    或者是 [_xxx release];


    这边有两个帖子,我觉得不错:
    链接1:stackoverflow.com/questions/7262268/why-shouldnt-i-use-the-getter-to-release-a-property-in-objective-c

    链接2:developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW13     
    --- 查看Don’t Use Accessor Methods in Initializer Methods and dealloc

  • 相关阅读:
    RabbitMQ安全相关的网络资源介绍
    种植玉米,发酵与生物燃料的制作
    一致哈希算法Java实现
    添加xml文件编辑语法提示
    【转】10分钟搭建NDK的Android开发环境
    【转】Windows环境下Android NDK环境搭建
    【转】蓝牙4.0——Android BLE开发官方文档翻译
    【转】java提高篇(十)-----详解匿名内部类
    【转】Android自定义View的实现方法,带你一步步深入了解View(四)
    【转】java枚举类型enum的使用
  • 原文地址:https://www.cnblogs.com/lvyinbentengzhe/p/4259154.html
Copyright © 2020-2023  润新知