memory problem with bi-directional referenced objects
memory problem with bi-directional referenced objects
- Subject: memory problem with bi-directional referenced objects
- From: Dirk van Oosterbosch <email@hidden>
- Date: Fri, 11 Mar 2005 12:49:05 +0100
Hi All,
I have some memory management questions relating two classes of object that point to eachother.
My two classes that refer to eachohter are IRSegment & IRViewSegment.
IRSegment has ivar IRViewSegment *myViewSegment; and
IRViewSegment has ivar IRSegment *segmentObject;
The IRViewSegments are maintained in my IRView class with the NSMutableArray segmentViewArray.
in IRSegment:
- (BOOL)registerView:(IRViewSegment *)myView
{
if (mySegmentView == nil) {
if (myView != nil) {
[self setMySegmentView:myView];
return YES;
}
}
return NO;
}
- (BOOL)detachView:(IRViewSegment *)myView
{
if (mySegmentView == myView) {
[self setMySegmentView:nil];
return YES;
}
return NO;
}
- (IRViewSegment *)mySegmentView{ return mySegmentView;}
in IRViewSegment:
- (IRSegment *)segmentObject{ return segmentObject;}
- (void)setSegmentObject:(IRSegment *)aSegmentObject
{
if (aSegmentObject != segmentObject) {
BOOL result;
if (aSegmentObject == nil) { // removing the link
result = [segmentObject detachView:self];
} else {
if (segmentObject == nil) { // setting new link
result = [aSegmentObject registerView:self];
} else { // set to some other IR Segment: first detach the old one
result = [segmentObject detachView:self];
if (result) {result = [aSegmentObject registerView:self];}
}
}
if (result) { // Succes?
[aSegmentObject retain];
[segmentObject release];
segmentObject = aSegmentObject;
} else {NSLog(@"Warning! It wasn't able to detach or register a View in IRSegment!"); }
}
}
- (void)dealloc
{
[self setSegmentObject:nil];
[super dealloc];
}
in IRView:
- (BOOL)updateGraph:(IRSegment *)fromObject
{
// First remove the graph if that one exist
[viewSegments removeAllObjects];
...
IRViewSegment *newView = [[IRViewSegment alloc] initWithBezierPath:aPath];
[newView setSegmentObject:aSegment];
}
And I realize I am leaking memory here. The -dealloc of IRViewSegment is never called.
Now I see multiple solutions, but they all seem hackish:
1. Don't have the IRSegment retain the IRViewSegment in -registerView:
2. Overwrite the -release of IRViewSegment and implement IRSegment's -detachView: there
3. Instead of [segmentViewsArray removeAllObjects] iterate through the Array and send all IRViewSegments another -release
4. Instead of [segmentViewsArray removeAllObjects] iterate through the Array and have IRViewSegments be -dealloc -ed
5. Instead of [segmentViewsArray removeAllObjects] iterate through the Array and send all IRViewSegments (viewSeg) [[viewSeg segmentObject] detachView: viewSeg];
How would you implement such a system with bi-directional referencing? I have the feeling I am making it more complicated than it has to be, but right now I am leaking memory ...
Any help?
Thanks,
dirk
_______________________________________________
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