autorelease

Creating autorelease objects in iPhone Development

烈酒焚心 提交于 2019-12-13 02:14:54
问题 I have a requirement to create some NSDecimalNumber objects objects as part of my application (as I require the precision of calculation they offer) but I note that in calculations they return NSDecimalNumber objects which are, presumably, autoreleased. My question is really whether this is potentially problematic in an iPhone application where I may carry out lots of calculations. The question is not just relating to NSDecimalNumber specifically but to the sometimes unavoidable creation of

Do variables assigned to properties follow the behavior of that property?

情到浓时终转凉″ 提交于 2019-12-13 01:29:02
问题 Looking for a little clarification on how Objective-C properties work when 'linked' to instance variables. My confusion stems from how you can set a property equal to a instance variable through the @synthesize directive, like... @synthesize someProp = _someIVar; Now, if my someProp is all like... @property (retain,readonly) SomeClass* someProp ...will this... -(id)initWithAutoreleasedInstanceOfSomeClass:(SomeClass*)thingThatIsAutoreleased { self = [super init]; if(self) { _someIVar =

How to return CFDataRef without memory leak?[ios]

丶灬走出姿态 提交于 2019-12-12 14:25:33
问题 When I return a CFDataRef by (CFDataRef)MyFunction{ ..... CFDataRef data = CFDataCreate(NULL, buf, bufLen); free(buf); return data; } There is a memory leak, how to make CFDataRef autorelease? the method [data autorelease] doesn't exit. 回答1: You can't autorelease Core Foundation objects. (However, you can autorelease Core Foundation objects that support toll-free bridging such as CFDataRef; see @newacct's answer below.) The Objective-C convention is to name your method such that it starts

What is the scope of (nested) autorelease pools?

怎甘沉沦 提交于 2019-12-12 12:18:59
问题 I'm creating an autorelease pool in a for loop (in method A). At each iteration of the loop, I'm calling another method (method B). Method B returns an autoreleased object to Method A. If I drain the pool within the for loop in Method A, will that release the objects sent from Method B? Thanks! 回答1: Yes - any time an object is sent -autorelease , it's added to the highest level autorelease pool. As long as you aren't creating any new autorelease pools in method B or further down the call

iPhone - substringToIndex / substringFromIndex / substringWithRange memory leak

青春壹個敷衍的年華 提交于 2019-12-11 17:57:01
问题 Instruments leaks says that this code leaks: NSString *name = [file substringToIndex:i]; Layer *actualLayer = nil; for (Layer *lay in layers) { if ([lay.layerName isEqual:name]) { actualLayer = lay; } } name is the leaking object. There are some strange things: it only leaks sometimes, not always (this snippet of code is executed hundreds of time during a normal execution of my app, but it leaks just 3-4 times). The other strange thing is that i suppose the name object to be an autoreleasing

MKReverseGeocoder autorelease/release question in Apple's CurrentAddress sample

久未见 提交于 2019-12-11 07:28:11
问题 I am looking at this code lifted straight from the MapViewController.m file in the CurrentAddress sample available on Apple's web site: - (void)dealloc { [reverseGeocoder release]; [mapView release]; [getAddressButton release]; [super dealloc]; } - (IBAction)reverseGeocodeCurrentLocation { self.reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease]; reverseGeocoder.delegate = self; [reverseGeocoder start]; } I am wondering what

Am I leaking this ObjectiveC Object?

天大地大妈咪最大 提交于 2019-12-11 06:41:32
问题 @interface foo: NSObject @property (nonatomic, retain) NSMutableArray *aMutableArray; @end @implementation @synthesize aMutableArray -(void)somefunction { // Illustration self.aMutableArray = [[[NSMutableArray alloc]init]autorelease]; self.aMutableArray = [[[NSMutableArray alloc]init]autorelease]; self.aMutableArray = [[[NSMutableArray alloc]init]autorelease]; } @end I have done code similar code to this in other parts of my program, but I needed to be certain that this does not cause a

Autorelease then retain for setters

别等时光非礼了梦想. 提交于 2019-12-11 00:27:00
问题 According to the Google Objective-C Style Guide, we should autorelease then retain as so: - (void)setFoo:(GMFoo *)aFoo { [foo_ autorelease]; // Won't dealloc if |foo_| == |aFoo| foo_ = [aFoo retain]; } In this case, foo_ will not be deallocated if being set to the same instance, making for a more defensive setter. My question is, is this how @property & @synthesize work? 回答1: release due to an autorelease isn't called until the end of the current runloop so foo_ wont dealloc because retain is

__weak and autorelease pool in ARC in Xcode 4.2

送分小仙女□ 提交于 2019-12-10 10:08:31
问题 My project use ARC. I tested with the code below: NSString __weak *string; @autoreleasepool { string = [NSString stringWithString:@"AAA"]; } NSLog(@"string: %@", string); I think it output as: string: (null) but it actually output: string: AAA I don't understand it. What is the effect of __weak? EDIT: And this code below: NSString __weak *string; NSString __strong *str; @autoreleasepool { str = [NSString stringWithFormat:@"%@", @"AAA" ]; string = str; } NSLog(@"string: %@", string); It also

What's the right way to set an NSError outparam from inside an autoreleasepool block?

拟墨画扇 提交于 2019-12-10 02:32:06
问题 I have a method like this: - (void)processAThing:(id)thing error:(NSError * __autoreleasing *)error { @autoreleasepool { // Start processing. // Find some partway through error.. if (error) { *error = [NSError errorWithDomain...]; return NO; } // More processing. } } This is broken and crashes, because the NSError is autoreleased, and when the return happens, the pool is drained, so the thing that the caller gets is now bogus. I know I could significantly redesign the method so I collect all