返回前释放局部变量?

发布于 2024-08-21 22:20:25 字数 377 浏览 8 评论 0原文

在 Objective-C 中,我知道您需要释放您初始化/保留/复制的任何内容。我需要在 return 语句之前执行此操作吗?我想了解显式调用 release 而不是使用 autorelease

-(void) someMethod
{
  AnotherClass* ac = [[AnotherClass alloc] init];
  if([ac somethingHappens]){
    // Do I need to release ac here?
    return;
  }
  [ac doSomethingElse];
  [ac release];
}

谢谢!

In objective-c, I understand that you need to release anything you init/retain/copy. Do I need to do that before a return statement? I'm wanting to understand calling release explicitly and not use autorelease.

-(void) someMethod
{
  AnotherClass* ac = [[AnotherClass alloc] init];
  if([ac somethingHappens]){
    // Do I need to release ac here?
    return;
  }
  [ac doSomethingElse];
  [ac release];
}

Thanks!

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

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

发布评论

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

评论(2

安人多梦 2024-08-28 22:20:25

是的,您需要释放变量,但是您退出该方法。

这非常简单:当您初始化某些内容时,保留计数就会增加。当你释放它时,它会减少。当它达到零时,它会自动释放(释放)。

在上面的代码中,您初始化了变量,但如果它遵循返回路径,则变量保留计数永远不会为零,因此永远不会被释放。

Yes, you need to release your variables, however you exit from the method.

It's pretty straight-forward: when you init something the retain count is incremented. When you release it's decremented. When it reaches zero it's automatically deallocated (freed).

In your code above, you init the variable but if it follows the return route then the variables retain count never gets to zero and, therefore, is never deallocated.

遥远的绿洲 2024-08-28 22:20:25

假设有一个像下面这样分配的局部变量

NSString *placeHolder = [NSString stringWithFormat:@"%@ %@",[someObject value1], [someObject value2]];

现在将此变量传递给对象定义的方法,例如 UISearchBar 对象的 setPlaceholder

[self.theSearchBar setPlaceholder:placeHolder];

如何以正确的方式释放分配的字符串 'placeHolder' ?

如果你想自动释放它:

NSString *placeHolder = [[NSString stringWithFormat:@"%@ %@",[someObject value1], [someObject value2]] autorelease];

你的代码将会失败并出现 bad_exc_access

如果你想在传递到其他地方后释放变量,比如

[self.theSearchBar setPlaceholder:placeHolder];
[placeHolder release];

运行时异常也会抛出。

那么出了什么问题呢?

问题是保留计数。 UISearchBar 对象尚未分配,因此如果您释放或自动释放该对象引用的此类变量,则保留计数仍然相同

NSLog(@"Retain count before assign to refer other object %d", [placeHolder retainCount]);
[self.theSearchBar setPlaceholder:placeHolder];
NSLog(@"Retain count after referencing %d", [placeHolder retainCount]);

那么,如何处理?

尝试类似以下的操作

[placeHolder retain]; // retainCount +1
[self.theSearchBar setPlaceholder:placeHolder];
[placeHolder release]; // retainCount -1

我们做了什么?现在让我们看一下保留计数。

NSLog(@"Retain count before doing retain %d", [placeHolder retainCount]);
[placeHolder retain]; // retainCount +1
NSLog(@"Retain count after retaining it %d", [placeHolder retainCount]);

因此,我们在将其分配(被引用)给某个对象之前增加了保留计数,然后 - 之后 - 我们在本地释放该变量。

就这样。

Suppose to have a local variable assigned like the following

NSString *placeHolder = [NSString stringWithFormat:@"%@ %@",[someObject value1], [someObject value2]];

Now pass this variable to a object defined method, such as setPlaceholder of UISearchBar object

[self.theSearchBar setPlaceholder:placeHolder];

How to release in the right way the assigned string 'placeHolder' ?

If you suppose to autoreleas it:

NSString *placeHolder = [[NSString stringWithFormat:@"%@ %@",[someObject value1], [someObject value2]] autorelease];

your code will fail with a bad_exc_access

If you think to release the variable after passed to somewhere else like

[self.theSearchBar setPlaceholder:placeHolder];
[placeHolder release];

a runtime exception will throw too.

So what's wrong?

The problem is the retain count. The UISearchBar object is allocated yet, so if you release or auto-release such a variable, referred by that object, the retain count is still the same

NSLog(@"Retain count before assign to refer other object %d", [placeHolder retainCount]);
[self.theSearchBar setPlaceholder:placeHolder];
NSLog(@"Retain count after referencing %d", [placeHolder retainCount]);

So, how to handle this?

Try something like the following

[placeHolder retain]; // retainCount +1
[self.theSearchBar setPlaceholder:placeHolder];
[placeHolder release]; // retainCount -1

What we did than ? Let's have a look at the retain count now

NSLog(@"Retain count before doing retain %d", [placeHolder retainCount]);
[placeHolder retain]; // retainCount +1
NSLog(@"Retain count after retaining it %d", [placeHolder retainCount]);

So, we incremented the retain count before assign it (get referenced by) to some object, and - after that - we release locally that variable.

That's all.

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