Manual reference counting and doubly-linked lists
Manual reference counting and doubly-linked lists
- Subject: Manual reference counting and doubly-linked lists
- From: William Squires <email@hidden>
- Date: Wed, 14 Nov 2012 21:59:46 -0600
Let's say I have:
@interface Thing : NSObject
@property (nonatomic, ???) Thing *nextThing;
@property (nonatomic, ???) Thing *prevThing;
@end
and somewhere I keep a reference to the 'head' of this doubly-linked list. What should ??? be, "retain", or "assign" in order to work properly? If I was using ARC instead, would they be "strong" or "weak"? Assuming the list doesn't contain circular references (i.e. a<->b<->c<->a) will releasing my 'head' reference properly clean up if I have:
"doublist.h"
@class Thing;
@interface DoublyLinkedListClass
@property (nonatomic, retain) Thing *head;
@end
"doublist.m"
@implementation DoublyLinkedListClass
@synthesize head = _head;
…
-(void)dealloc
{
self.head = nil; // Is this begging for a problem?
[super dealloc];
}
…
@end
One way would be to walk the doubly-linked list and copy (the references) into an NSMutableArray, and set the "nextThing" and "prevThing" instance variables to nil as I go, then (with the list objects disconnected), release the reference to the NSMutableArray. Is this overkill to avoid a leak? In this case, the Thing objects are already in an NSMutableArray that's part of a higher-level model; in fact, they're loaded into the array first, then I call a private method of my DungeonModel class (the one that has the mutable array) to "connect" the object references based on two NSInteger properties in Thing (not shown above for clarity) that refer to the objects to be connected via the index of their location in the mutable array.
A pity OS X wasn't in existence when I took all those Comp Sci. classes in college, or maybe they might have had a course in "Data Structures using Objective-C"… Ha! :)
_______________________________________________
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