How to tell if object is in an NSAutoreleasePool
I would like to know how many times an object has been autoreleased. I've used objective c long enough that it's generally straight forward to know whether an object has been autoreleased or not, however I constantly see questions dealing with memory and retain counts. At some point an answer always ends, "You can't trust the retainCount of an object" - which I agree with, BUT if you could determine how many times an object has been autoreleased, then you actually could trust the retainCount if you added a开发者_运维知识库 category like:
@interface NSObject (NSObject_MemoryDebugging)
- (NSUInteger) autoReleaseCount;
- (NSUInteger) retainCountWithAutoRelease;
@end
@implementation]
/** Determine how many times this object has been marked for autorelease **/
- (NSUInteger) autoReleaseCount;
{
// ??? not sure how to figure this out.
return 0;
}
- (NSUInteger) retainCountWithAutoRelease
{
NSUInteger retainCount = [self retainCount];
NSUInteger autoReleaseCount = [self getAutoReleaseCount]; // ???
return retainCount - autoReleaseCount;
}
@end
There would still be an exception for immutable types as those typically increase the retain count during a copy, so you still can't trust retainCount on those.
What I am NOT proposing
I am not seeking this answer to use retainCount in production code. However, I can see this as being valuable for someone debugging memory issues.
I imagine some people will HATE this question since programmers should not care about how many times an object has been autoreleased. Coding should be all about balancing allocs, retain, copy, new with release, end of story. However, the point of this is to help out people banging their heads. [NSObject retainCount]
burns a lot of people, and an answer to this question would be pretty cool.
I'm certain there's a way to determine how many times an object has been autoreleased. I just don't know what it is, hence the question.
See similar question: Objects inside NSAutoreleasePool in objective-c.
Edit
Thank you everyone for your answers. You may find this interesting => Ariel pointed out that GNUStep's implementation of Cocoa and specifically it's NSAutoReleasePool has this method: +(NSUInteger)autoreleaseCountForObject:(id)anObject. This method is slow, and only returns the autorelease count from NSAutoReleasePools on the callers thread. Still... It's interesting that its there. The docs cite that it is really only useful for debugging. This is really what I was hoping to find (or find possible) in the Cocoa framework somehow.
I agree with the answers given that even if it were possible to get the autorelease count that better tools exist (Zombies, Leaks, static analyzer).
First, you'd have to deal with multiple autorelease pools and an object being autoreleased more than once, possibly in multiple pools.
Second, it's not (just) NSAutoreleasePool
that's making -retainCount
untrustworthy. The problem is that all sorts of objects, both yours and Apple's, retain things for all sorts of reasons, most unbeknownst to you. Even accounting for autorelease, your objects will often not have the retain count you expect, because behind the scenes, something's observing it or placing it in a dictionary temporarily, etc.
The best ways to debug memory issues are the Leaks instrument, NSZombie, and the static analyzer.
No, there isn’t a public API to determine whether an object has been autoreleased.
Even if this were publicly available, your -retainCountWithAutoRelease
method would have some problems:
An object can be placed multiple times in the same autorelease pool so you’d need an autorelease count for a single autorelease pool instead of a flag indicating whether an object has been autoreleased;
An object can be placed in multiple autorelease pools due to multithreading, so you’d need an autorelease count spanning multiple autorelease pools;
Due to multithreading, you’d need to synchronise your code with Cocoa’s handling of retain counts and autorelease pools, and the internal locking used by Cocoa is not visible to applications.
NSAutoreleasePool does have an +(void)showPools
method that I was previously completely unaware of. This could be useful. Also see Is there a way to inspect an NSAutoreleasePool's objects?. On that thread, KennyTM said (in a comment):
Well, since the content is printed to stderr, you could reopen the stream and parse from it to get all the pointers
For reference, I used class-dump against the Foundation framework to see it's NSAutoreleasePool details and it had the following:
@interface NSAutoreleasePool : NSObject {
void *_token;
void *_reserved3;
void *_reserved2;
void *_reserved;
}
+ (void)addObject:(id)arg1;
+ (id)allocWithZone:(struct _NSZone *)arg1;
+ (void)showPools;
+ (void)releaseAllPools;
+ (unsigned int)autoreleasedObjectCount;
+ (unsigned int)topAutoreleasePoolCount;
+ (BOOL)autoreleasePoolExists;
+ (void)enableRelease:(BOOL)arg1;
+ (void)enableFreedObjectCheck:(BOOL)arg1;
+ (unsigned int)poolCountHighWaterMark;
+ (void)setPoolCountHighWaterMark:(unsigned int)arg1;
+ (unsigned int)poolCountHighWaterResolution;
+ (void)setPoolCountHighWaterResolution:(unsigned int)arg1;
+ (unsigned int)totalAutoreleasedObjects;
+ (void)resetTotalAutoreleasedObjects;
- (id)init;
- (void)drain;
- (oneway void)release;
- (id)initWithCapacity:(unsigned int)arg1;
- (void)addObject:(id)arg1;
- (id)retain;
- (unsigned int)retainCount;
- (id)autorelease;
- (void)dealloc;
@end
I've added this as an answer as it seemed more of an answer than more detail on the question I asked.
Sounds like you'll need to override -(id)autorelease;
method as the work of adding object to NSAutoreleasePool
done there.
Something like that:
-(id)autorelease{
_isAutoreleased = YES; //some BOOL member initialized to NO and returned on -(BOOL)isAutoreleased;
[NSAutoreleasePool addObject:self];
return self;
}
Also take a look at this link
精彩评论