Arrays containing container -- retain cycle?
Arrays containing container -- retain cycle?
- Subject: Arrays containing container -- retain cycle?
- From: Matt Budd (Madentec) <email@hidden>
- Date: Thu, 2 Dec 2004 11:38:01 -0700
Hello all,
I have an issue with arrays containing the object that contains those
arrays. It seems to cause a retain cycle and nothing gets dealloc'ed.
Imagine I am dealing with the following class (psuedo-code):
@interface X : NSObject {
int MyVal;
X *nextX;
}
//Accessor methods....
@end
I know that when I set the nextX member in its accessor method, that I
should set it only a reference and not retain it (since that could
cause a retain cycle).
However, if I have the following class (using a link-array instead of a
single link-pointer):
@interface Y : NSObject {
int MyVal;
NSMutableArray *nextYs; //of class Y
}
//Accessor methods....
@end
@implementation Y
- (id)init {
if (self = [super init]) {
MyVal = -1;
nextYs = [[NSMutableArray alloc] init];
}
return self;
}
- (NSMutableArray *)getNextYs {
return nextYs;
}
@end
And say I have the following code executing in some action:
Y *yObject1 = [[Y alloc] init];
[yObject1 setMyVal: 15];
Y *yObject2 = [[[Y alloc] init] autorelease];
[yObject2 setMyVal: 16];
[[yObject1 getNextYs] addObject: yObject1];
[[yObject1 getNextYs] addObject: yObject2];
This causes the problem. In this case yObject1 never gets dealloc'ed
(and neither does yObject2 since the nextYs array of yObject1 only gets
released in the dealloc of yObject1).
Basically, I want to just have an array of references (like the first
example of the single links, but with multiple items), instead of
having NSMutableArray retain it's objects and then try to release them
when it dealloc's. Is there any work around?
- Matt
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden