保留合成属性的计数

发布于 2024-11-03 16:10:31 字数 844 浏览 4 评论 0原文

请查看我的代码:

@interface ClassA : NSObject {
    ClassB *objB;
}

@property (retain) ClassB *objB;
@end

@implementation ClassA:
@synthesiaze objB;
@end

int Main(int argc, const char *argv[])
{
    ClassA *objA = [[ClassA alloc] init];
    ClassB *objB = [[ClassB alloc] init];

    NSLog(@"%d", (int)[objB retainCount]);    // 1
    NSLog(@"%d", (int)[[objA objB] retainCount]);     // 0

    objA.objB = objB;

    NSLog(@"%d", (int)[objB retainCount]);    // 2
/* --> */    NSLog(@"%d", (int)[[objA objB] retainCount]);     // 3
    NSLog(@"%d", (int)[[objA objB] retainCount]);     // 4
    NSLog(@"%d", (int)[objB retainCount]);    // 4
}

请查看第 23 行, NSLog(@"%d", (int)[[objA objB] keepCount]);

我认为结果应该是 2 而不是 3,但是每个一次,调用 [objA objB] 似乎将保留计数增加了 1。我不知道发生了什么。谁能告诉我?谢谢!

Please review my code:

@interface ClassA : NSObject {
    ClassB *objB;
}

@property (retain) ClassB *objB;
@end

@implementation ClassA:
@synthesiaze objB;
@end

int Main(int argc, const char *argv[])
{
    ClassA *objA = [[ClassA alloc] init];
    ClassB *objB = [[ClassB alloc] init];

    NSLog(@"%d", (int)[objB retainCount]);    // 1
    NSLog(@"%d", (int)[[objA objB] retainCount]);     // 0

    objA.objB = objB;

    NSLog(@"%d", (int)[objB retainCount]);    // 2
/* --> */    NSLog(@"%d", (int)[[objA objB] retainCount]);     // 3
    NSLog(@"%d", (int)[[objA objB] retainCount]);     // 4
    NSLog(@"%d", (int)[objB retainCount]);    // 4
}

Please look at line 23, NSLog(@"%d", (int)[[objA objB] retainCount]);

I think the result should be 2 not 3, but every time, calling [objA objB] seems to increase the retain count by 1. I don't know what's happening. Who can tell me? Thanks!

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

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

发布评论

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

评论(3

椵侞 2024-11-10 16:10:31

苹果有这样的说法:retainCount

重要提示:此方法通常对于调试内存管理问题没有任何价值。因为任意数量的框架对象可能保留了一个对象以保存对其的引用,而同时自动释放池可能在对象上保存任意数量的延迟释放,因此您不太可能从中获得有用的信息方法。

要了解必须遵守的内存管理基本规则,请阅读 “内存管理规则”

不用担心保留计数;如果您调用 retainalloc 或名称以 copy、< 开头的方法,只需担心调用 release 即可。代码>mutableCopy,或new

Apple has this to say about retainCount:

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

To understand the fundamental rules of memory management that you must abide by, read “Memory Management Rules”.

Don't worry about retain counts; just worry about calling release if you call retain, alloc, or a method whose name starts with copy, mutableCopy, or new.

风铃鹿 2024-11-10 16:10:31

首先,不要依赖 retainCount 始终保持 100% 准确。

也就是说,您所看到的只是因为合成的 getter 看起来像这样:

- (ClassB *)objB
{
    return [[objB retain] autorelease];
}

因此,当您通过合成的 getter 请求对象时,它会被保留并自动释放。这是因为从非所有权 getter 获得的任何内容都应该在当前自动释放池的生命周期内持续存在,但如果您在此期间释放了 objA,那么情况就不会如此。

First of all, don't rely on retainCount to always be 100% accurate.

That said, what you're seeing is just because the synthesised getter looks like this:

- (ClassB *)objB
{
    return [[objB retain] autorelease];
}

So, when you ask for the object through a synthesised getter, it is retained and autoreleased. That's because anything you get from a non-ownership getter is supposed to last for the life of the current autorelease pool, but if you released objA in the interim then that wouldn't be the case.

踏雪无痕 2024-11-10 16:10:31

你的@property没有被标记为非原子,所以getter不仅仅是一个简单的返回,而是一个锁、保留、自动释放和解锁——出于线程安全的目的。您可以编写自己的 getter 或将属性声明为 @property(非原子,保留)

Your @property is not marked as nonatomic, so the getter is not just a simple return, but a lock, retain, autorelease and unlock - for thread safety purposes. You can either write your own getter or declare the property as @property (nonatomic, retain)

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