Re: ARC vs Manual Reference Counting
Re: ARC vs Manual Reference Counting
- Subject: Re: ARC vs Manual Reference Counting
- From: Dave <email@hidden>
- Date: Tue, 10 Sep 2013 19:46:59 +0100
On 9 Sep 2013, at 09:49, Marcel Weiher <email@hidden> wrote:
>
> The pattern I adopted long ago to avoid that sort of situation is to have an instance variable for my temps, in which case the code becomes:
>
> [self setTemp:newObject];
> … do stuff …
> [self setTemp:nil];
>
> or if you prefer dot syntax:
>
> self.temp = newObject;
> … do stuff …
> self.temp = nil;
>
> Even if you forget nilling, you at most have an extended lifetime of an object, not a leak. I also generally do the same in initialization code (but not in dealloc). For me, that simply got rid of reference-counting pain. Completely. Memory management is mediated by accessors, always. And accessors are generated.
>
This is great except that you lose the type information for the Class in question? A way around it might be to have one Instance Variable and a number of properties of the Right Type, but you'd have to define these in an @interface section which would be messy.
e.g.
@interface someClass ()
{
id mTempIVar;
}
@property (nonatomic,retain) NSArray* pTempArray;
@property (nonatomic,retain) NSNumber* pTempNumber;
@end
@implementation someClass
@synthesize pTempArray = mTempIVar;
@synthesize pTempNumber = mTempIVar;
You'd also have to watch out that you didn't reuse the same "temp" in your methods.
I toyed with the idea of writing a LocalMemoryManager class:
+(id) allocLocalObjectOfClass:(Class) theClass forInstance:(id) theCallingObject andMethod:(NSString*) theMethodName;
+(void) releaseLocalObjectsForClass:(Class) theClass andMethod:(NSString*) theMethodName;
+(void) releaseAllLocalObjectsForClass:(Class) theClass;
You'd call it like this:
NSString* myString;
myString = [[LocalMemoryManager allocLocalObjectOfClass:[NSString Class] forInstance:self andMethod:__FUNCTION__] initWithString@"xxxxx"];
Place a call to releaseLocalObjectsForClass at end of method to release all local objects and in dealloc call releaseAllLocalObjectsForClass.
I just couldn't bear the thought of having to use a long wordy method call like allocLocalObjectOfClass to allocate objects.
> I am starting to think that this may explain the (vast) difference in perception of ARC, at least it’s an explanation I can understand: if you made the switch to always having reference counting mediated by accessors, RC goes away as a pain point. If you haven’t, it’s probably a huge pain that ARC removes.
Agreed I have my own way of handling local storage and use a different pattern, which, sometimes involves the dreaded goto!
Cheers
Dave
_______________________________________________
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