1

Is there a nice way to do this in Objective-C, or do I have to write my own tedious logic?

I'm creating and destroying a little of little state objects per frame in an iPhone game. It would be nice if I could just reuse objects from a pool.

3 Answers3

7

The Sparrow framework for the iPhone contains a class called "SPPoolObject". The framework uses it internally for helper objects that are used extremely often, like points, rectangles or matrices.

If you inherit from SPPoolObject, the 'dealloc' method does not really delete it; instead, the memory will be reused for the next alloc'ed object.

It's quite a simple class - you can easily use it for your projects. It does not have any dependency, so you can easily grab it out of the Sparrow framework ;)

PrimaryFeather
  • 479
  • 3
  • 12
1

Neither Cocoa nor Objective-C does anything particularly helpful for object pools. They don't do anything to stop you either, but you'll basically have to DIY.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • What about creating a memory zone? Am I mistaken in thinking that this works as a sort of pool? – Ranking Stackingblocks May 04 '10 at 23:27
  • 1
    They can be useful in this situation, but they're very distinct from object pools, at least as I understand them. Memory zones can conceivably make allocation more efficient and help keep related objects contiguous, but you still allocate and deallocate as normal inside them. – Chuck May 05 '10 at 00:04
0

I think that the table View has some sort of pooling mechanism for the different TableCell

something like that :

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                    cellId];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CheckMarkCellIdentifier] autorelease];
    }

Not sure if there is something more generic to use.

Itay Levin
  • 1,579
  • 2
  • 16
  • 23