Do I have to send save or autoadvertising before returning objects?

I thought I was doing everything here, but I get a few warnings from assembly and analysis, so now I'm not sure. My assumption: (a) that the object that I get from the function (dateFromComponents: in this case) is already set to autorelease and (b) that what I return from the function should be set to autorelease. Therefore, I do not need to send auto-advertisement or save the result of dateFromComponents: before I return it to the caller. Is it correct?

As a side note, if I rename my function from newTimeFromDate: to gnuTimeFromDate, the analyzer does not give any warnings about this function. Is this a convention that all the "new" methods return a stored, not an auto-implemented object?

The Cocoa Memory Management Programming Guide says: "The received object is usually guaranteed to remain within the method that was received" and that "this method can also safely return the object to its caller." This leads me to believe that my code is correct.

However, Cocoa's “Memory Management” says: “Suppose that objects obtained by any other method have a retention value of 1 and are in the auto-detection pool. If you want to keep it outside the current execution volume, you must save it.” This makes me think that I need to do a save before returning the NSDate object.

I am developing from Xcode 3.2.1 to 10.6.2, focusing on the iPhone SDK 3.1.2.

assembly / analysis screenshot http://nextsprinter.mggm.net/Screen%20shot%202009-11-15%20at%2008.33.00.png

Here's the code if you have trouble reading a screenshot:

//============================================================================ // Given a date/time, returns NSDate for the specified time on that same day //============================================================================ +(NSDate*) newTimeFromDate:(NSDate*)fromDate Hour:(NSInteger)hour Minute:(NSInteger)min Second:(NSInteger)sec { NSCalendar* curCalendar = [NSCalendar currentCalendar]; const unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents* comps = [curCalendar components:units fromDate:fromDate]; [comps setHour: hour]; [comps setMinute: min]; [comps setSecond: sec]; return [curCalendar dateFromComponents:comps]; } 
+6
memory-management objective-c iphone cocoa-touch
source share
1 answer

You are almost right. The only problem clang talks about well is that your promises method has an object count count +1 (it contains “new” for its name), but you are returning an object with auto-implementation.

You have two options: removing the “new” from the method name or saving the return object. Its much more Cocoa -ish to return an auto-implemented object (like you) and call the timeFromDate: method.

+8
source share

All Articles