简体   繁体   English

我应该在返回对象之前发送保留或自动释放吗?

[英]Should I send retain or autorelease before returning objects?

I thought I was doing the right thing here but I get several warnings from the Build and Analyze so now I'm not so sure. 我以为我在这里做的是正确的,但我从构建和分析中得到了一些警告,所以现在我不太确定。 My assumption is (a) that an object I get from a function (dateFromComponents: in this case) is already set for autorelease and (b) that what I return from a function should be set for autorelease. 我的假设是(a)我从函数中获取的对象(在这种情况下为dateFromComponents:)已经设置为自动释放,(b)我应该从函数返回的对象设置为autorelease。 Therefore I don't need to send autorelease or retain to the result of the dateFromComponents: before I return it to the caller. 因此,在将其返回给调用者之前,我不需要发送自动释放或保留dateFromComponents的结果: Is that right? 是对的吗?

As a side note, if I rename my function from newTimeFromDate: to gnuTimeFromDate the analyzer does not give any warnings on this function. 作为旁注,如果我将我的函数从newTimeFromDate重命名为:gnuTimeFromDate,分析器不会对此函数发出任何警告。 Is it the convention that all "new*" methods return a retained rather than autoreleased object? 是所有“new *”方法返回保留而不是自动释放的对象的约定?

In Memory Management Programming Guide for Cocoa it says "A received object is normally guaranteed to remain valid within the method it was received" and that "That method may also safely return the object to its invoker." Cocoa的内存管理编程指南中,它说“收到的对象通常保证在收到它的方法中保持有效”,并且“该方法也可以安全地将对象返回给它的调用者”。 Which leads me to believe my code is correct. 这让我相信我的代码是正确的。

However, in Memory Management in Cocoa it says "Assume that objects obtained by any other method have a retain count of 1 and reside in the autorelease pool. If you want to keep it beyond the current scope of execution, then you must retain it." 但是,在Cocoa的Memory Management中,它说“假设通过任何其他方法获得的对象的保留计数为1并驻留在自动释放池中。如果要保持超出当前执行范围,则必须保留它。 “ Which leads me to think I need to do a retain before returning the NSDate object. 这让我觉得我需要在返回NSDate对象之前进行保留。

I'm developing with Xcode 3.2.1 on 10.6.2 targeting the iPhone SDK 3.1.2. 我正在使用针对iPhone SDK 3.1.2的10.6.2上的Xcode 3.2.1进行开发。

screenshot of build/analyze output http://nextsprinter.mggm.net/Screen%20shot%202009-11-15%20at%2008.33.00.png 构建/分析输出的屏幕截图http://nextsprinter.mggm.net/Screen%20shot%202009-11-15%20at%2008.33.00.png

Here's the code in case you have trouble reading the screen shot: 如果您在阅读屏幕截图时遇到问题,请输入以下代码:

//============================================================================
// 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];
}

You are nearly right. 你几乎是对的。 The only problem that clang correctly points out is that your method promises a retain count +1 object (for its name containing “new”) but you are returning an autoreleased object. clang正确指出的唯一问题是你的方法承诺一个retain count +1对象(其名称包含“new”),但是你要返回一个自动释放的对象。

You have two options: removing the “new" from the method name or retaining the returned object. It's far more Cocoa-ish to return the autoreleased object (as you do) and name the method timeFromDate: . 您有两个选择:从方法名称中删除“new”或保留返回的对象。返回自动释放的对象(正如您所做的)更多Cocoa-ish并将方法命名为timeFromDate: .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是否需要保留闭包中引用的自动释放对象? - Do I need to retain autorelease objects that are referenced in closures? 我应该在返回表单 -(NSString*) 描述之前自动释放吗? - Should I autorelease before return form -(NSString*)description? 通过使用保留来获得自动释放对象的所有权? - Taking ownership of autorelease objects by using retain? @property(保留)是否自动释放或释放对象? - Does @property (retain) autorelease or release objects? 我应该将传递的vars / object排除在自动释放池之外吗? - Should I leave passed vars/objects out of the autorelease pool? 在将对象添加到集合之前使用自动释放? - Use autorelease before adding objects to a collection? 我应该在分配给保留属性的自动释放对象上调用释放,然后再重新分配它吗? - Should I call release on an autorelease object assigned to a retained property before reassigning it? 为什么我应该在setter方法中将-autorelease发送给我的实例变量,而不是-release? - Why should I send -autorelease to my instance variable in my setter method, rather than -release? 在getter方法中保留和自动释放 - retain and autorelease in a getter method 自动释放,然后保留给二传手 - Autorelease then retain for setters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM