FROM : Matt Budd (Madentec)
DATE : Thu Dec 02 19:38:01 2004
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
DATE : Thu Dec 02 19:38:01 2004
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
| Related mails | Author | Date |
|---|---|---|
| Matt Budd (Madente… | Dec 2, 19:38 | |
| Scott Stevenson | Dec 2, 21:14 |






Cocoa mail archive

