简体   繁体   English

Objective C内存泄漏问题

[英]Objective C memory leak issue

The Leaks instrument tells me that I have a leak in this code fragment. 泄漏工具告诉我,我的代码片段有泄漏。 Why is this so? 为什么会这样?

This code fragment is in viewDidLoad() . 此代码片段位于viewDidLoad()

UINavigationItem *navItem=[self navigationItem];

UIBarButtonItem *addFeed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFeed)];
[navItem setRightBarButtonItem:addFeed]; // leaks says that 128 bytes leaked
[addFeed release];

UIBarButtonItem *reload = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh  target:self action:@selector(reload)];
[navItem setLeftBarButtonItem:reload]; // leaks says that 128 bytes leaked here too !
[reload release];
[navItem release];

You should not be releasing navItem . 你不应该发布navItem You did not alloc/retain/new/create it, so you do not release it. 您没有分配/保留/新建/创建它,因此您不会释放它。

Other than that, your code looks fine. 除此之外,您的代码看起来很好。 Is that everything in the method? 是方法中的一切吗?

The leaks instrument only tells you where the leaked memory was allocated; 泄漏仪器只告诉你泄漏的内存分配在哪里; it can't tell you where the memory should have been released but wasn't since there's no possible way for it to know that. 它无法告诉你应该释放内存的位置但是因为没有可能的方法让它知道。 Your leak is occurring elsewhere. 你的泄漏正在其他地方发生

This code is mostly fine, except you should not be releasing navItem at the end. 这段代码大部分都没问题,除非你不应该在最后发布navItem You are not an owner of it, since you didn't create it with a method named alloc , new , or copy in its name, so you should not release it. 您不是它的所有者,因为您没有在其名称中使用名为allocnewcopy的方法创建它,因此您不应该发布它。

if you're still getting the leak message and can't track down the bug, you can try using the static analyzer included in the latest and greatest Xcode (version 3.2) 如果您仍然收到泄漏消息并且无法追踪该错误,您可以尝试使用最新最好的Xcode(版本3.2)中包含的静态分析器

Build > Build and Analyze 构建>构建和分析

it'll use LLVM-Clang to statically analyze your code in a pretty way. 它将使用LLVM-Clang以一种漂亮的方式静态分析您的代码。

http://developer.apple.com/mac/library/featuredarticles/StaticAnalysis/index.html http://developer.apple.com/mac/library/featuredarticles/StaticAnalysis/index.html

UPDATE: 更新:

in your code snippet: 在您的代码段中:

UINavigationItem *navItem=[self navigationItem];

UIBarButtonItem *addFeed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFeed)];
[navItem setRightBarButtonItem:addFeed]; // leaks says that 128 bytes leaked
[addFeed release];

your leak is probably coming from setting the new rightBarButtonItem without releasing the old one. 您的泄漏可能来自设置新的rightBarButtonItem而不释放旧的。

this is what i think is happening: 这就是我认为正在发生的事情:

1) get a handle to the navigationItem (has right bar button A) 1)获取navigationItem的句柄(右边的条形按钮A)

2) create a new UIBarButton Item (making right bar button B) 2)创建一个新的UIBarButton项目(制作右侧栏按钮B)

3) setRightBarButtonItem to button B 3)setRightBarButtonItem为按钮B

now where's Button A? 现在按钮A在哪里? it should have been released by navItem when you set the new button. 设置新按钮时应该由navItem释放。 so you could have forgotten to release the button when you set it the first time, or you've got it retained somewhere else. 因此,当您第一次设置按钮时,您可能忘记释放按钮,或者您已将其保留在其他位置。

Do you have NSZombieEnabled? 你有NSZombieEnabled吗? This causes objects not to be retained by the NSZombie instances and you'll see "leaks" when running the Leaks tool. 这会导致NSZombie实例不保留对象,并且在运行Leaks工具时会看到“泄漏”。

您似乎没有使用自定义viewDidLoad方法释放视图控制器。

 [navItem setRightBarButtonItem:addFeed];

 [navItem setLeftBarButtonItem:reload];

You are creating copies of the objects in these accessors. 您正在创建这些访问器中的对象的副本。 These accessors are incrementing the retainCount by 1. Your accessors should release each object and then immediately retain them. 这些访问器将retainCount递增1.您的访问器应释放每个对象,然后立即保留它们。

Example: 例:

- (void) setTitle: (NSString*) newTitle {
    if (title != newTitle) {
        [title release];
        title = [newTitle retain]; // Or copy, depending on your needs.
    }

Take a look at the techniques here: Memory Management Programming 看看这里的技术: 内存管理编程

I believe that's what's up. 我相信那是什么了。 So take a hard look at those two accessors. 所以仔细看看这两个访问者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM