Save / copy auto-implemented objects

I want to make sure that I understand memory management correctly here. Is there any specific reason to use one of the assignCurrentDate methods over the other here? In addition, all this leads to a memory leak, right?

in .h we have:

NSDate *currentDate1; NSDate *currentDate2; NSDate *currentDate3; NSDate *currentDate3; //and @property (nonatomic, retain) NSDate *currentDate1; @property (nonatomic, retain) NSDate *currentDate2; @property (nonatomic, retain) NSDate *currentDate3; @property (nonatomic, retain) NSDate *currentDate4; 

in .m:

 -(void) assignCurrentDate1 { currentDate1 = [[NSDate date]retain]; //[NSDate date] is autoreleased } -(void) assignCurrentDate2 { currentDate2 = [[NSDate date]copy]; } -(void) assignCurrentDate3 { self.currentDate3 = [NSDate date]; } -(void) assignCurrentDate4 { currentDate4 = [[NSDate alloc]init]; //[[NSDate alloc]init] is not autoreleased. } -(IBAction) printDate { NSLog ("%@", currentDate1); NSLog ("%@", currentDate2); NSLog ("%@", currentDate3); NSLog ("%@", currentDate4); } - (void)dealloc { [currentDate1 release]; [currentDate2 release]; [currentDate3 release]; [currentDate4 release]; [super dealloc]; } 
+4
source share
2 answers

The rule of thumb when it comes to iOS memory management:

For each alloc , retain , copy or new , you must have a corresponding release or autorelease .

In fact, you flow in several places. In your header, retain your date objects, and then in the dealloc method, you release them. It's right. However, in your assignDate methods, you cannot release a copy or a stored date. While [NSDate date] auto-implemented, you save and copy them yourself.

There is no reason to use your assignCurrentDate methods. In the init method, you can simply do something like the following:

 self.currentDate1 = [NSDate date]; 

What is it.

Edit: (Well, that is not the case.)

As Jim notes in the comments:

Saving in the header means that the synthesized installer for these properties will save the objects assigned to them. But if you look at the assign * methods, you will see that only the CurrentDate3 property actually uses the property. The rest is assigned directly to ivar, bypassing the synthesized setter, so they are not saved when assigned.

+6
source

Yes, you understand memory management correctly. None of these leaks, assuming you are not calling methods more than once. The second option is less efficient in terms of memory usage, since two NSDate instances are NSDate . In fact, they all differ slightly in performance, but not significantly unless you put them in tight loops.

In terms of developing a program, you would not want to write such code, because if you ever call 1, 2, or 4 times more, the initially allocated instance will leak. If you are sure that this is not a problem (for example, if you assign in viewDidLoad and release in viewDidUnload ), then you can use any of these styles, but if you are not sure if it is, you need to protect your task by releasing it to the destination , or you should use a third property-based method that does this for you.

+2
source

All Articles