7

Just wondering if this is even possible, maybe I could retrieve an NSSet of objects that are currently stored in a given NSAutoreleasePool instance?

I have looked through Apple's NSAutoreleasePool reference and have not found much pertaining to this question.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • possible duplicate of [Objects inside NSAutoreleasePool in objective-c](http://stackoverflow.com/questions/3181578/objects-inside-nsautoreleasepool-in-objective-c) – kennytm Jan 28 '11 at 08:14
  • 1
    @Kenny [We're tending to let similar questions stand, now.](http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/). –  Jan 28 '11 at 13:23

2 Answers2

11

If the purpose is just for debugging, you could use the function _CFAutoreleasePoolPrintPools() to print the content of the pool, as documented in TN2124. It is available since Mac OS X 10.6 and iOS maybe since 3.x.

Alternatively, the class method [NSAutoreleasePool showPools] performs the same action.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Oh wow. This is awesome. In fact, this is **exactly** what I was looking for! :) – Jacob Relkin Jan 28 '11 at 08:23
  • Yeah, this is nice - it pretty-prints a list of objects currently in the pool, but it can't return a collection of objects. :( – Jacob Relkin Jan 28 '11 at 08:29
  • @Jacob: The structure that stores the objects can be retrieved by `pthread_getspecific(0x3e)`, but there is no guarantee that the structure will not be changed across platforms since this is some internal API. Well, since the content is printed to stderr, you could reopen the stream and parse from it to get all the pointers. – kennytm Jan 28 '11 at 10:08
  • Could you provide an example of that? Thanks. – Jacob Relkin Jan 28 '11 at 19:59
  • 1
    Hey guys - is showPools deprecated in 4.2? I cannot find a mention of it in documentation as existing or ever existing and the compiler does not find it. – mobibob Apr 21 '12 at 13:38
  • Just for others that stumble across this... showPools is a private class method on NSAutoreleasePool which means it will give you build warnings and won't show up in the list of available methods. Just put in the code and you'll get the contents of the pool on the current thread listed in your debugging output. – Red Nightingale May 03 '16 at 09:58
2

No, there is not. The implementation details of NSAutoreleasePool are deep black magic.

What you might do instead is to make your own autorelease pool and somehow fool the runtime into thinking that it should use your pool instead of NSAutoreleasePool. But only do this for the thrill of trying it.

HOWEVER, YOU SHOULD NEVER ALTER AUTORELEASE BEHAVIOR FOR ANYTHING OTHER THAN EXPERIMENTATION AND LEARNING. OTHERWISE I WILL FIND YOU. AND MAKE YOU REWRITE YOUR CODE.

Community
  • 1
  • 1
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • @Dave I think I *will* do that! :-) – Jacob Relkin Jan 28 '11 at 08:21
  • @Dave **Don't worry - this is all experimental and will never make it into production - I understand the dangers of this, thank you! :P** – Jacob Relkin Jan 28 '11 at 08:24
  • 2
    @Jacob I know you'll be fine; this more of a surgeon general's warning to anyone else who comes along and happens to think I might actually be seriously suggesting this. – Dave DeLong Jan 28 '11 at 08:25
  • @Dave LOL. Surgeon general's warning **indeed**. – Jacob Relkin Jan 28 '11 at 08:26
  • @Dave Here's the question I was talking about last night: http://stackoverflow.com/questions/5035877/grab-all-values-in-nsdictionary-inside-an-nsarray – Jacob Relkin Feb 23 '11 at 21:12
  • @Jacob yep; `NSExpression` (used internally by `NSPredicate`) ends up calling `-valueForKeyPath:` on the target object, which ends up invoking `-valueForKey:`, which [does some special stuff](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/uid/20000140-BBCIBCDJ) for `NSDictionary`. – Dave DeLong Feb 23 '11 at 21:23