• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Maintaining a List of instances of a class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Maintaining a List of instances of a class


  • Subject: Re: Maintaining a List of instances of a class
  • From: Rainer Brockerhoff <email@hidden>
  • Date: Wed, 18 Jul 2007 12:10:24 -0300

At 06:37 -0700 18/07/2007, email@hidden wrote:
>From: "Paolo Manna" <email@hidden>
>Date: Wed, 18 Jul 2007 14:32:11 +0100
>Message-ID: <email@hidden>
>
>>how do I let a class maintain a list (MutableArray) of it's own instances?
>>In C++ I would do this just in the constructor.
>You can try with - (id)init, the nearest thing you have to a
>constructor: something like
>...
>However, please be aware that there's a catch: if you're not careful,
>your object could end up to be around forever! The instance list will
>retain the object even when every other has released it: that's why,
>incidentally, you can't remove the object from the array during
>dealloc - while it's in the array, dealloc isn't called at all! You
>can possibly end up checking the retainCount every time you call
>release, and removing  from the array when it gets to 1 (that is, only
>the array is keeping the object around).

One solution which avoids checking retainCount (something to be avoided whenever possible) would be using a non-retaining NSMutableSet to hold the instances, like this (warning:compiled in mail):

static NSMutableSet* instances = NULL;

+ (void)initialize {
	if (!instances) {
		instances = (NSMutableSet*)CFSetCreateMutable(kCFAllocatorDefault,0,NULL);
	}
}

and in your init... method you'd do something like:

- (id)init {
	self = [super init];
	if (self) {
		[instances addObject:self];
	}
	return self;
}

and then in your dealloc method:

- (void)dealloc {
	[instances removeObject:self];
	[super dealloc];
}

Depending on what you need this list of instances for, you might have to implement some of the CFSet callbacks; what do you want to do with it?

HTH,
--
Rainer Brockerhoff  <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
 In their own business even sages err."
Weblog: http://www.brockerhoff.net/bb/viewtopic.php
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please 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:

This email sent to email@hidden

  • Prev by Date: Re: Maintaining a List of instances of a class
  • Next by Date: Re: Sorting issues with NSTableView
  • Previous by thread: Re: Maintaining a List of instances of a class
  • Next by thread: CIFilters on a bezeirpath
  • Index(es):
    • Date
    • Thread