由于保留计数而释放对象

发布于 2024-09-27 16:52:53 字数 704 浏览 2 评论 0 原文

我遇到了多次发布视图的问题。虽然理论上很简单,因为我将 uiview 移动到 uiview,它是 uiview 的子类并且是动画的等等,但它不是我可以轻松修复的东西。它只有 10% 的崩溃率,并且仅在某些条件下崩溃,即使在这些条件下,也只有 30% 的崩溃率。

换句话说,它有点复杂。有时在我的 dealloc 方法中,此 UIView 的保留计数已经是 1(当视图被释放时它被释放),因此不应再次释放。所以我所做的是:

if ([mainView retainCount] > 1) {
    NSLog(@"released");
    [mainView release];
}

与发布的崩溃一致通常会被调用,但并非总是如此,当我有时预计它会崩溃时,它就会发生。我用这段代码检查了泄漏情况,它从未泄漏。

现在真正的问题...由于保留计数而释放某些内容是否是错误的?我尝试了许多不同的方法来解决这个问题,到目前为止,这是唯一可靠且不泄漏的方法。

编辑:如果不是,那么将一个 UIView 复制到另一个 UIView 的更好方法是什么?

mainView = newView;
[newView release];

我尝试先释放 mainView,然后在 newView 上调用副本,但这会崩溃。除了保留计数有时比预期低 1 之外,上面的代码也可以完美运行,尽管它从未在代码中的任何其他地方释放过。

I have an issue with releasing a view too many times. Although simple in theory because im moving uiview to uiview, which is a subclass of uiview and being animated and so on its not something that I can easily fix. It only crashes 10% and only under certain conditions and only 30% of the time even under these conditions.

So in other words its kinda complex. Sometimes in my dealloc method the retain count of this UIView is already 1 (which gets released when the view is released) and so shouldn't be released again. So what I did is this:

if ([mainView retainCount] > 1) {
    NSLog(@"released");
    [mainView release];
}

Consistant with the crashes released is usually called, but not always and it happens pretty much when I would sometimes expect it to crash. I've checked for leaks with this code and it never leaks.

Now the actual question... Is it wrong to release something due to its retain count? I've tried many different ways to fix this and so far this is the only reliable and non leaking one.

EDIT: If no then what is the better way to copy one UIView to another UIView?

mainView = newView;
[newView release];

I've tried releasing the mainView first then calling copy on the newView but this crashes. The above also works perfectly except the retain count is sometimes 1 lower than expected even though its never released ANYWHERE else in the code.

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

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

发布评论

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

评论(3

时光礼记 2024-10-04 16:52:53

不要使用retainCount值。

说真的,你不应该永远将该值用于任何像这样真正有用的事情。

如果您有内存泄漏,或由于过度释放而崩溃,请修复它们 - 它们是错误!这不是处理它们的方法。

编辑:总是值得一读:内存管理指南

Do not use the retainCount value.

Seriously, you should never use that value for anything really useful like this.

If you have memory leaks, or experience crash due to overreleases, fix them - they are bugs! And this is not the way to handle them.

Edit: Always a good read: Memory Management Guide

叫嚣ゝ 2024-10-04 16:52:53

不要使用-retainCount。

对象的绝对保留计数是没有意义的。

您应该调用 release 的次数与导致对象被保留的次数完全相同。不能少(除非你喜欢泄漏),当然也不能多(除非你喜欢崩溃)。

请参阅内存管理指南了解完整详细信息。


在这种特定情况下,在没有保留对象时释放对象会导致灾难。如果它现在没有崩溃,有一天它也会崩溃,可能是由于软件更新或看似不相关的更改。它可能现在才起作用,因为你的内存管理在其他地方都是错误的。

如前所述,这:

mainView = newView;
[newView release];

不是复制视图。更糟糕的是,它过度释放了视图(因为你无处保留它)。

在视图上使用 copy 方法不是正确的解决方案,或者至少是非常不典型的。 UI 元素不会以这种方式复制。

您是否尝试过构建和分析并修复了它指示的任何错误?

泄漏可能不会显示任何内容,因为可能仍然有一个指向泄漏对象的指针漂浮在可访问的内存中。

Do not use -retainCount.

The absolute retain count of an object is meaningless.

You should call release exactly same number of times that you caused the object to be retained. No less (unless you like leaks) and, certainly, no more (unless you like crashes).

See the Memory Management Guidelines for full details.


In this specific case, releaseing an object when you did not retain it is a recipe for disaster. If it doesn't crash now, it will someday, possibly due to a software update or seemingly unrelated change. It might only be working now because your memory management is wrong everywhere else.

As was stated, this:

mainView = newView;
[newView release];

Is not making a copy of the view. Worse, it is over-releasing the view (because nowhere do you retain it).

Using the copy method on views is not the right solution or, at least, would be exceedingly atypical. UI elements are not copied in that fashion.

Have you tried build and analyze and fixed any errors it indicates?

leaks may not show anything because there may still be a pointer to the leaked object floating about in reachable memory.

陌上青苔 2024-10-04 16:52:53
mainView = newView;

它不是副本,而是作业。保留计数不会增加。那么你就不必发布。

mainView = newView;

it is not a copy but an assignment. The retainCount will be not increase. Then you don't have to make a release.

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