简体   繁体   English

在对象的类的方法中释放对象是否可以接受

[英]Is it acceptable to release object in a method of the object's class

Is it acceptable for a instance method of a class to release itself? 类的实例方法释放自身是否可以接受?

ie to have a method that calls: 即具有一个调用以下方法的方法:

[self release]

Assume that my code is not going to access self after calling [self release] 假设我的代码在调用[self release]后将无法访问self。

You should only do this if you've done something like 仅当您已完成以下操作时才应这样做

[self retain];

But it's unclear why you would do that. 但目前尚不清楚为什么要这么做。 The Cocoa Memory Management Documentation might help 可可内存管理文档可能会有所帮助

While I doubt that immediately after your release, memory would move much, keep in mind that the code that your [self release] is in, resides in a memory block inside your object, self. 尽管我怀疑释放后内存会立即移动很多,但请记住,[自我释放]所在的代码位于对象(自我)内部的内存块中。 Thus, it is possible that after returning from [self release], you end up in code that is no longer allocated and is being written over by some other process. 因此,有可能从[self release]返回后,您最终将得到不再分配的代码,并正在被其他进程覆盖。 Can't say for sure how probable that is, but it seems possible. 不能肯定地说那有多大可能,但似乎有可能。

I have used this technique once before, for a similar situation (standalone object handling response from a web delegate that may outlive the view that launched the request). 对于类似的情况,我曾经使用过这种技术(来自Web委托的独立对象处理响应可能超过了发出请求的视图)。

It does work, but is actually rather tricky to get right. 它确实有效,但实际上正确起来相当棘手。 Since then I have found that using NSOperations in an NSOperationQueue is a much more solid and well-understood approach to encapsulating background actions that run independent of the requestors. 从那时起,我发现在NSOperationQueue中使用NSOperations是封装独立于请求者运行的后台操作的一种更为可靠且易于理解的方法。 Usually when an operation is done a notification is sent out on the main thread informing whatever caller might still be around that data is ready for pickup. 通常,当操作完成后,会在主线程上发出通知,通知任何调用者可能仍然在身边,准备接收数据。

Plus for simple remote requests you can use the simpler synchronous URL calls in your Operation since they run in a separate thread and will not block the main thread while data is incoming (handy when fetching small images from URL's, for example). 另外,对于简单的远程请求,您可以在Operation中使用更简单的同步URL调用,因为它们在单独的线程中运行,并且在传入数据时不会阻塞主线程(例如,从URL提取小图像时很方便)。

Is it acceptable for a instance method of a class to release itself? 类的实例方法释放自身是否可以接受?

ie to have a method that calls: 即具有一个调用以下方法的方法:

[self release] [自我发布]

Assume that my code is not going to access self after calling [self release] 假设我的代码在调用[self release]后将无法访问self。

First, I would want to have a really good reason to release myself. 首先,我想有一个很好的理由释放自己。 The only time I've done it is in a singleton that I dump to free up large chunks of memory on an iPhone. 我唯一一次这样做是在单例中,我转储以释放iPhone上的大块内存。 This is a rare event. 这是罕见的事件。

Your code is part of the class object. 您的代码是类对象的一部分。 Hence, it is not really a problem to call [self release]. 因此,调用[self release]并不是真正的问题。 Of course, you are much safer, from an encapsulation perspective, if you call [self autorelease]. 当然,从封装的角度来看,如果调用[self autorelease],则更加安全。 At least then, if someone up the call chain calls your methods, you don't cause an exception. 至少然后,如果调用链中的某人调用了您的方法,则不会引起异常。

Andrew 安德鲁

You can do it. 你能行的。 It works. 有用。 It is a bit dangerous, especially since optimizing compilers can rearrange your code in ways you didn't intend them to. 这有点危险,特别是因为优化编译器会以您不希望的方式重新排列代码。

A little bit safer is to call [self autorelease], which will release the current object at some point in the near future (the next time through the runloop typically) rather than right away. 更加安全的是调用[self autorelease],它会在不久的将来(通常是下次通过运行循环)在某个时候释放当前对象,而不是立即释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM