或者[自我释放]工作?

发布于 2024-11-11 13:32:26 字数 538 浏览 0 评论 0原文

我制作了一个使用相机解码条形码的对象。如果成功,它会向下一步操作发送委托消息,如果没有,它只会消失,然后下一个屏幕开始工作。

如果我在对象的最后一个命令中,我可以发送[self release]来释放内存吗?但是,问题是:retainCount 可以不止一个吗?

就像

  ObjectToDealloc *test = [[ObjectToDealloc alloc] init];
  [test doYourJob];
  //can't release here, it broke my program

这个对象所做的最后一件事一样,它运行:

  -(void)destroyYourself {
     [self release];
     [self dealloc];
  }

这会起作用吗?我会释放内存吗?或者我必须在全局可见性(不仅仅是函数可见性)中进行“测试”并执行以下操作:

   [test release];

I make a object that use the camera to decode a bar code. If it was successful it send a delegate message to next action, if not, it only go out, and next screen start to work.

If am I in the last command of the object, can I send [self release] to free the memory? But, the problem is: the retainCount can be more than on one?

Like

  ObjectToDealloc *test = [[ObjectToDealloc alloc] init];
  [test doYourJob];
  //can't release here, it broke my program

in the last thing that this object do, it run:

  -(void)destroyYourself {
     [self release];
     [self dealloc];
  }

This will work? I will release the memory? Or I have to make "test" in global visibility (not only function visibility) and do a:

   [test release];

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

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

发布评论

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

评论(2

蝶舞 2024-11-18 13:32:26

这不是正确的内存管理。在少数情况下,[self release] 是正确的。 [self dealloc] 在任何情况下都是正确的(除了您自己的 dealloc 中的超类之外,您永远不应该调用任何人的 dealloc)。

当你上面说“它破坏了我的程序”时,你是什么意思? ObjectToDealloc 是否执行某种后台处理?如果没有,那么您应该始终能够在您指定的位置释放它。如果它确实进行后台处理,那么您需要一些东西来保留该对象直到完成,然后释放它。通常,最好的方法是保留和释放 self,但通常您还有其他东西来完成它,例如 NSOperationQueue 等。

这里更可能的问题是您在 ObjectToDealloc 中错误地处理内存管理,并且当它解除分配时会崩溃。什么是崩溃?

要记住的关键一点是,你永远不要在 ObjC 中说“立即销毁这个对象”。你说“我已经完成了这个物体”。当每个人都使用完该对象后,它就会被运行时销毁。

This is not correct memory management. There are a small number of cases when [self release] is correct. There are no cases where [self dealloc] is correct (you should never call anyone's dealloc except your superclass's in your own dealloc).

When you say "it broke my program" above, what do you mean? Does ObjectToDealloc perform background processing of some sort? If it does not, then you should always be able to release it at the point you indicate. If it does do background processing, then you need something to retain the object until it is done, and then release it. Every so often, the best approach is to retain and release self, but usually you have some other thing do it like an NSOperationQueue or the like.

The more likely issue here is that you are handling memory management incorrectly in ObjectToDealloc and that when it deallocates it crashes. What is the crash?

The key thing to remember is that you never say "destroy this object right now" in ObjC. You say "I'm done with this object." When everyone is done with the object, then it is destroyed by the runtime.

ˇ宁静的妩媚 2024-11-18 13:32:26

在一些例子中,我看到一些类会保留自己,以便在执行工作之前保留自己。 永远、永远、永远不要调用 DEALLOC*。 Dealloc 不适合你,而是适合运行时。如果不再需要某个对象,只需适当释放它即可。调用该方法只是运行时的职责。

下面是我经常使用的一些代码示例,其中对象保留自身,然后在完成其工作后适当地释放自身: UIAlertView+Blocks 此代码执行此操作是因为即使在实例化类已释放该对象以便其可以运行并完成之后,该对象也应该保留下来给它执行的块。

*除非在您自己的 dealloc 中调用超级的 dealloc,但这不言而喻。

There are a couple of instances where I have seen classes that retain themselves in order to keep themselves around until they perform work. NEVER, EVER, EVER CALL DEALLOC*. Dealloc is not for you, but the runtime. If you no longer need an object, just appropriately release it. It is only the runtime's duty to call that method.

Here's an example of some code that I regularly use where the object retains itself and then appropriately releases itself when done with its work: UIAlertView+Blocks This code does this because the object is suppose to stick around, even after it has been released by the instantiating class so that it can run and finish the blocks it was given to execute.

*Except when calling your super's dealloc within your own dealloc, but this goes without saying.

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