Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: observing dealloc




On May 28, 2007, at 12:19 PM, Jim Correia wrote:

I think you are going to have to be more specific about what it is you are trying to do, rather than the mechanics by which you think you should do it.

In particular, since NSDictionary copies its keys, trying to observe the object lifetime of the original instance isn't going to do you any good.

I'm writing a base class and want to include a built in capability for contextual globals. For example:


@implementation MyObject

NSMutableDictionary		*gGlobals = nil;

+ (void) initialize
{
	if (gGlobals == nil)
	{
		gGlobals		= [[NSMutableDictionary alloc] init];
	}
}

+ (NSMutableDictionary *) globalsForKey:(id) inKey
{
	NSMutableDictionary		*result	= [gGlobals objectForKey: inKey];
	
	if (result == nil)
	{
		result 		= [NSMutableDictionary dictionary];
		[ gGlobals setObject: result
			forKey: inKey];
	}

	return result;
}

@end

In the above, the key is an object that serves as a context for a set of globals and when that context disappears, I'd like to remove all associated globals as well.

On May 28, 2007, at 12:23 PM, Shawn Erickson wrote:

You have two issues. 1) NSDictionary copies keys that you use and 2) NSDictionary holds a retain on those keys (related to issue 1). So dealloc will not get called.

Ah yes, a catch 22.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >observing dealloc (From: Ken Tozier <email@hidden>)
 >Re: observing dealloc (From: Jim Correia <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.