处理从 cocoa 便捷方法返回的对象

发布于 2024-07-07 21:43:41 字数 662 浏览 5 评论 0原文

我遇到了很多 NSDate 对象过早释放的问题。 我怀疑这些问题可能与我处理从 NSDate 便捷方法返回的对象的方式有关。 我认为 JKShow 类中的 showDate 属性声明应该是“保留”,但将其更改为分配或复制似乎对问题没有影响。

JKShow *show; 
NSDate *date;
NSMutableArray *list = [[NSMutableArray alloc] init];

// Show 1
show = [[JKShow alloc] init];
//...
date = [gregorian dateFromComponents:dateComponents];
show.showDate = date;
[list addObject:[show autorelease]];

// Show 2
show = [[JKShow alloc] init];
//...
date = [gregorian dateFromComponents:dateComponents];
show.showDate = date;
[list addObject:[show autorelease]];

更新

问题不在此处复制的代码中。 在我的 JKShow init 方法中,我没有保留从 NSDate 便捷方法返回的日期。 谢谢大家的帮助。

I'm having a lot of issues with NSDate objects being prematurely deallocated. I suspect that the issues may be related to the way that I deal with the objects returned from NSDate convenience methods. I think that my showDate property declaration in the JKShow class should be "retain", but changing it to assign or copy seems to have no effect on the issue.

JKShow *show; 
NSDate *date;
NSMutableArray *list = [[NSMutableArray alloc] init];

// Show 1
show = [[JKShow alloc] init];
//...
date = [gregorian dateFromComponents:dateComponents];
show.showDate = date;
[list addObject:[show autorelease]];

// Show 2
show = [[JKShow alloc] init];
//...
date = [gregorian dateFromComponents:dateComponents];
show.showDate = date;
[list addObject:[show autorelease]];

UPDATE

The issue was not in the code copied here. In my JKShow init method I was not retaining the date returned from the NSDate convenience method. Thanks for your help, everyone.

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

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

发布评论

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

评论(4

日暮斜阳 2024-07-14 21:43:41

从 dateFromComponents 返回的日期应该位于自动释放池中,因此您的 showDate 属性应该是“保留”是正确的。 事实上它应该是无论如何(除非你特别想要“复制”)。

从您显示的代码来看,您似乎将显示对象的所有权完全交给了列表(因为您在添加它们时在它们上设置了自动释放)。 您是说日期对象在显示对象从列表中出来之前被释放(或者列表正在被释放)?

另外,您是使用合成属性,还是手动编写它们? 如果是后者,你的 setShowDate 属性方法是什么样的?

您还可以尝试在不同的位置记录日期对象的保留计数(尽管我总是发现自动释放确实使事情变得复杂)。

The date returned from dateFromComponents should be in the autorelease pool, so you are correct that your showDate property should be "retain". In fact it should be anyway (unless you specifically want "copy").

From the code you have shown it looks like you are giving ownership of your show object entirely to the list (as you're setting autorelease on them as you add them). Are you saying that the date objects are being deallocated before the show objects are coming out of the list (or the list is being deallocated)?

Also, are you using synthesised properties, or are you writing them by hand? If the latter, what is your setShowDate property method like?

You can also try logging the retainCount of the date object at different places (although I always find that autorelease really complicates that).

み青杉依旧 2024-07-14 21:43:41

如果 showDate 是一个保留属性,那么考虑到您发布的代码,这应该足够了。 其他东西(可能在 JKShow 的实现中)可能不正确。

如果您想弄清楚发生了什么,可以使用 Instruments 来检查对象的寿命。 您需要使用分配工具集来运行它以记住保留和释放。 默认情况下,如果您运行泄漏性能工具,则会以这种方式设置。

当您像这样运行 Instruments 时,它将记录所有对象的生命周期,以及针对它们发出的每个保留和释放的回溯。 如果您查看对象,找到其中一个日期,并查看所有保留和释放,您应该能够确定虚假释放发生在哪里。

If showDate is a retain property that should be sufficient, given the code you have posted. Something else (probably in JKShow's implementation) may not be correct.

If you want to figure out what is going on, you can use Instruments to see examine the objects lifespan. You need to run it with the allocation tool set to remember retains and releases. By default it is set up that way if you run the leaks performance tool.

When you run Instruments like that it will record all object life spans, and the backtrace for every retain and release issued against them. If you look through the objects, find one of your dates, and look at all the retains and releases you should be able to determine where the spurious release is happening.

稀香 2024-07-14 21:43:41

您显示的代码没有过早发布的问题。 事实上,它会泄漏数组及其中的所有内容,因为它不会释放数组。

您是否在垃圾收集器打开的情况下运行?

list 是实例变量还是静态变量,还是局部变量?

The code you showed has no premature-release problems. In fact, it will leak the array and everything in it, because it doesn't release the array.

Are you running with the garbage collector turned on?

Is list an instance variable or static variable, or is it a local variable?

南薇 2024-07-14 21:43:41

我想通了,感谢您的帮助,但问题出在我在这里发布的代码之外。 我没有保留在 init 方法中创建的 NSDate 。 不幸的是,直到我创建了两个新的 NSDate 对象之后,崩溃才发生,所以我完全找错了树。

I figured it out, thanks for all your help, but the problem was outside of the code I posted here. I was not retaining the NSDate I created in my init method. Unfortunatly the crash didn't occur until after I had created the two new NSDate objects, so I was totally barking up the wrong tree.

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