Array of weak reference

Ken Anderson lists at anderhome.com
Sun Aug 15 01:29:42 PDT 2004


Serge,

I suggest you steal from EOF's bag of tricks and have a 'get rid of 
weak references' cycle before releasing the entire object graph.  When 
you're about to release the object(s), call a method all of your 
objects (in a superclass) implement like:

- (void) releaseWeakReferences
{
	[weakReferences 
makeObjectsPerformSelector:@selector(releaseWeakReferences)];
	[weakReferences release];
	weakReferences = nil;
}

This way, you don't have to have any messy interactions with NSArray, 
and you don't have to worry about objects being released out from 
underneath the NSArray.  Any attempt to wrap NSArray could lead to 
disaster, because NSArray will try to release any objects that are in 
it - so messing with the retain count could have NSArray sending 
-release to an already released object.

Another option is to use NSValue to wrap each individual object before 
putting it into the array:

NSValue *value = [NSValue valueWithNonretainedObjectValue:myObj];
[array addObject:value];

and when you get the object:

value = [array objectAtIndex:x];
myObj = [value nonretainedObjectValue];

You could always wrap the NSArray with your own object that put objects 
into it with NSValue, and then extracts them before returning them to 
your calling method.

Ken


On Aug 15, 2004, at 12:02 AM, Serge Cohen wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi List;
>
> I have a pretty complicated data model, in which there is a lot of 
> interconnection, hence I need to manage quite a few 'weak reference' 
> (pointer to object WITHOUT retaining them, to avoid circular 
> reference).
>
> I have the following problem: One of my object (in it's class 
> definition) need to have an array of weak reference... the problem is 
> that if I use NSArray (or NSMutableArray) all objects added to the 
> array are sent a retain... which I don't want.
> The only solution I can see is to:
>
> add object to array (+1 retain count from the array)
> release object (0 retain count from the array) -> weak reference
>
> When removing from the array I  have to make sure that I do it:
> retain the object (+1 retain count)
> remove from array (0 retain count).
>
> This seems complicated to me, and not very secure, so I'm hoping that 
> I'm missing one (easier) possibility...
>
> All insight/pointer to doc/comments... much welcomed.
>
> Serge.
>
> *******************************************
> Serge Cohen
>
> GPG Key ID: 9CBB58FB
> *******************************************
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.3 (Darwin)
>
> iD8DBQFBHuBG5EPeG5y7WPsRAhh0AKC8i0NQJ07e6QphD6cbfrYfn8BqcACdGcFn
> D4XD92ePcbBglv3j7f1iykE=
> =rNLd
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> MacOSX-dev mailing list
> MacOSX-dev at omnigroup.com
> http://www.omnigroup.com/mailman/listinfo/macosx-dev




More information about the MacOSX-dev mailing list