Jeremy L
Jeremy L

Reputation: 3810

Can an NSAutoreleasePool be drained twice or multiple times?

In the book I'm reading, it talks about forcing an NSAutoreleasePool to drain. Does this mean creating a local NSAutoreleasePool and then draining it, instead of draining the one that already exists? That is, can I not drain an NSAutoreleasePool twice or multiple times?

Upvotes: 0

Views: 221

Answers (1)

Kurt Revis
Kurt Revis

Reputation: 27994

No, you cannot drain an autorelease pool more than once.

does it mean creating a local NSAutoreleasePool and then drain it

Yes. Either make a new pool and drain it, or (even better) use @autoreleasepool.

we cannot drain a NSAutoreleasePool twice or multiple time?

Correct. The documentation for drain states:

In a reference-counted environment, this method behaves the same as release. Since an autorelease pool cannot be retained (see retain), this therefore causes the receiver to be deallocated.

Since an autorelease pool gets deallocated after the first call to drain, it is never valid to call any method on it afterwards, drain included.

Upvotes: 2

Related Questions