UITextView setEnabled:导致保留计数增加?

发布于 2024-10-27 12:45:07 字数 664 浏览 2 评论 0原文

我有一个 UITextView,它的文本集来自局部变量。在此之后是否有可能:

[textView setEnabled:NO];

保留计数增加一?

编辑:

“问题”,如果它是一个问题,是在一个小视图的构造函数中:

- (id)initWithData:(NSMutableArray *) {
  UITextView *myText;
  if ( ( self = [super init] ) ) {
    myText = [[UITextView alloc] initWithFrame:aRect];
    // retain count = 1;
    [myText setEnabled:NO]; // retain count 2
    [self addSubview:myText]; // retain count 3
    [myText release];  // retain count 2
  }
}

现在,我已经“自动释放”了文本视图,但我不确定内存是否管理良好(该帖子与此问题相关)。

I have a UITextView which has its text sets from a local variable. Is it possible that after this:

[textView setEnabled:NO];

The retain count gets incremented by one?

EDIT:

The "problem", if it is a problem, is in the constructor of a small view:

- (id)initWithData:(NSMutableArray *) {
  UITextView *myText;
  if ( ( self = [super init] ) ) {
    myText = [[UITextView alloc] initWithFrame:aRect];
    // retain count = 1;
    [myText setEnabled:NO]; // retain count 2
    [self addSubview:myText]; // retain count 3
    [myText release];  // retain count 2
  }
}

Now, I've "autoreleased" the text view, but I'm not sure if the memory is well managed (the post is related to this question).

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

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

发布评论

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

评论(1

思念满溢 2024-11-03 12:45:07

可能的?绝对地。

你关心吗?一点也不。除非您在子类中覆盖 setEnabled: 并且您是进行 retain 调用的人,否则不会。

如果是,则它是框架中的实现细节。


将保留计数视为增量,而不是绝对数字。绝对值没有意义。因此:

- (id)initWithData:(NSMutableArray *) {
  UITextView *myText;
  if ( ( self = [super init] ) ) {
    myText = [[UITextView alloc] initWithFrame:aRect]; // rc +1
    [myText setEnabled:NO]; // rc change irrelevant
    [self addSubview:myText]; // rc change irrelevant
    [myText release];  // rc -1
  }
}

所以,是的,你已经正确地管理了内存;在 myText 局部变量范围的末尾,所有保留都已通过释放进行了平衡。

当您 addSubview: 时,该方法是否保留该对象或复制它是一个与此范围内的内存管理无关的实现细节。显然,子视图维护引用并保留它对于其目的来说是必要的,但这是超出此方法内存管理范围的实现细节!

Possible? Absolutely.

Do you care? Not a bit. Not unless you overrode setEnabled: in a subclass and you are the one making the retain call.

If it is, it is an implementation detail in the frameworks.


Think about retain counts as deltas, not as absolute numbers. The absolute value is meaningless. Thus:

- (id)initWithData:(NSMutableArray *) {
  UITextView *myText;
  if ( ( self = [super init] ) ) {
    myText = [[UITextView alloc] initWithFrame:aRect]; // rc +1
    [myText setEnabled:NO]; // rc change irrelevant
    [self addSubview:myText]; // rc change irrelevant
    [myText release];  // rc -1
  }
}

So, yes, you have managed memory correctly; at the end of the scope of the myText local variable, all retains have been balanced by releases.

When you addSubview:, whether that method retains the object or makes a copy of it is an implementation detail that isn't relevant to memory management in this scope. Obviously, that the subview maintains a reference and retains it is necessary for its purposes, but that is an implementation detail beyond the scope of this method's memory management!

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