开发者

Can we use NSAutoreleasePool for a view?

I want to know is there any way to 开发者_StackOverflow社区use NSAutoreleasePool through a view (just like we define something in .h file and dealloc them in dealloc method of .m file).

Is it possible?


Nope, this shouldn't be done.

From Apple's autorelease pools documentation:

An autorelease pool should always be drained in the same context (such as the invocation of a method or function, or the body of a loop) in which it was created.

and the next paragraph

Autorelease pools are used “inline.” There should typically be no reason why you should make an autorelease pool an instance variable of an object.


Sure, you can. I am not sure if it makes sense in your situation, so you'll have to analyze that, but if you want:

In the .h file

@interface MyView : UIView
{
    NSAutoReleasePool *pool;
}
// rest of view

In the .m file:

@implementation MyView

- (id) initXYZ // whatever initializer you have...
{
    self = [super init...];
    if (self)
    {
        pool = [[NSAutoReleasePool alloc] init];
        // rest of initialization
    }
    return self;
}

- (void) dealloc
{
    // rest of dealloc
    [pool drain];
    [super dealloc];
}

As I said, I am not sure if it makes sense, unless you allocate lots of small objects in your view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜