释放时会发出内存泄漏警告,但自动释放时不会发出内存泄漏警告

发布于 2024-10-20 18:44:06 字数 614 浏览 7 评论 0原文

返回对象后释放对象时,我遇到内存泄漏警告问题。我读过一些关于类似主题的帖子,但在这些帖子中,发布的问题是,最终“他们”并没有真正拥有他们要发布的对象。

如果我在初始化同一对象时使用自动释放,我不会遇到任何问题。我的问题是:如果 Apple 建议手动释放我们创建的所有对象,我为什么会收到此警告?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

    //Display no cells until it loads
    if([items count] < numberOfItemsToDisplay){

        UITableViewCell *cell = [[[UITableViewCell alloc] init]autorelease]; 
        return cell;
        //[cell release];

    }

我在自动释放对象方面没有问题,但我讨厌不理解我认为我理解的东西:)

谢谢,L

I have a problem with memory leak warning when releasing an object after returning it. I read a few posts about the similar subject but in those posts problem with releasing was that in the end "they" didn't really own the objet they were releasing.

If I use autorelease while initializing that same object I get no problems. My question is: if Apple suggests releasing manually all the objects that we created, how come I get this warnings?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

    //Display no cells until it loads
    if([items count] < numberOfItemsToDisplay){

        UITableViewCell *cell = [[[UITableViewCell alloc] init]autorelease]; 
        return cell;
        //[cell release];

    }

I have no problems autoreleasing objects but I hate not understanding things I thought I understood :)

Thanks, L

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

依 靠 2024-10-27 18:44:06

Apple 不会说“手动释放您创建的所有对象”——他们只是说“最终释放您创建的所有对象”。这可以通过手动释放或自动释放来完成。

自动释放基本上只是稍后发生的手动释放(在 [NSAutoreleasePool排出][NSAutoreleasePool release] 被调用时)。

如果你希望你的方法之一返回一个由调用者拥有的对象,你必须使用 autorelease,因为如果你在“返回”之前调用release,并且保留计数会变为零(如果你已经刚刚创建了对象),那么该对象将立即被释放,然后不可用。该方法的调用者发现自己有一个指向某个垃圾非对象的指针。

Apple don't say "manually release all the objects you create" -- they just say "release eventually all the objects you create". That can be done by a manual release or by an autorelease.

An autorelease is basically just a manual release that occurs at some later point (at the point [NSAutoreleasePool drain] or [NSAutoreleasePool release] gets called).

If you want one of your methods to return an object that is owned by the caller, you have to use autorelease because if you call release before your 'return' and the retain count goes does to zero (which it usually will if you've just made the object) then the object will immediately be dealloc'd and then unusable. The caller of the method finds themselves with a pointer to some garbage non-object.

苍白女子 2024-10-27 18:44:06

return 语句后函数返回并且不再执行任何操作。所以return后release不会执行。所以你会泄漏内存。并且您不能在返回之前释放,因为调用者将使用该对象。所以你真的不能在返回之前释放它。所以你有两种方法来处理这种情况。第一个选项是使返回的对象自动释放。另一种选择是使函数名称的调用者知道它拥有返回的对象并且必须释放它。

检查 从内存管理编程指南中的方法返回对象了解此案例的详细说明。

After return statement function is returned and nothing further is executed. So release after return will not execute. So you will leak memory. And you can not release before the return as the caller will use that object. So you really can't release that before return. So you have two ways to handle this situation. 1st option is to make the returned object autorelease. Another option is to make the function name in such a way that the caller know that it owns the returned object and must release it.

Check Returning Objects from Methods from Memory Management Programming Guide for the details explanation of this case.

凶凌 2024-10-27 18:44:06

当您返回单元时,您在该点之后不再拥有该对象,因此您无法释放它。当执行 return 语句时,函数返回到调用它的位置。所以return之后的语句不会被执行。因此,当您在return之后释放cell时,它不会被执行,这意味着cell没有被释放。因此它会导致内存警告。

When you return cell u do not own the object after that point and hence u cannot release it. When the return statement is executed the functions returns to the point from where it was called. So the statements after the return is not executed. So when u release the cell after the return its not executed which means the cell is not released. Hence it causes a memory warning.

终弃我 2024-10-27 18:44:06

在 xcode 中添加一个 UitableView 类,它将自动正确添加委托以重用 tableviewcells。

Add a UitableView class in xcode and it will automatically add delegates correctly for the reuse of tableviewcells.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文