简体   繁体   English

在Objective-C中保留计数和工厂方法

[英]Retain count and factory methods in Objective-C

I have been in this forum before to find the best way of creating a factory function to construct a custom view from a nib ( here is my previous post) 我之前曾在这个论坛上找到过创建工厂函数以从笔尖构造自定义视图的最佳方法( 是我以前的文章)

i am now using the following code: 我现在正在使用以下代码:

+ (LoadingV *)loadingViewCopyFromNib 
{   
    LoadingV *view = nil;   
    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"LoadingV" owner:self options:nil];
    view = (LoadingV*)[nibViews objectAtIndex: 0];

    // Setting up properties
    CGRect frm = view.progress.frame;
    frm.size.width *=1.5;
    frm.size.height *=1.5;
    view.progress.frame = frm;
    view.waitLbl.text = NSLocalizedString(@"Please wait", @"");     
    return view;    <------- warning is here
}

// In .h file
...
LoadingView* loadV;
@property (nonatomic, retain) LoadingView* loadV;

// in .m file
@synthesize loadV;
...
self.loadV = [LoadingV loadingViewCopyFromNib];

When i Build and Analyse i get the following warning about the factory function: 当我进行构建和分析时,我收到有关工厂功能的以下警告:

/LPAPP/Classes/LoadingV.m:34:5 Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected /LPAPP/Classes/LoadingV.m:34:5具有+0保留计数的对象返回给调用方,其中预期+1(拥有)保留计数

Why is this happening? 为什么会这样呢? I understand that local variables allocated within a function do not live beyond its scope unless they are retained and autoreleased. 我知道,在函数内分配的局部变量除非保留并自动释放,否则不会超出其范围。 But in my case i am not creating a new object am just returning a reference to an existing one. 但就我而言,我不是在创建新对象,而只是返回对现有对象的引用。 So why am i getting warning back? 那么,为什么我会收到警告? Is it safe to proceed like this :) 这样安全吗:)

Cheers AF 干杯AF

Although Mike is right, the warning has a completely different reason. 尽管迈克是正确的,但警告的原因却完全不同。 Your method name includes "copy" which is interpreted to return a +1 retain count (similar to alloc, init). 您的方法名称包括“ copy”,它被解释为返回+1保留计数(类似于alloc,init)。 Remember that once you transition to ARC this may cause problems! 请记住,一旦过渡到ARC,这可能会引起问题!

When it comes to collection classes like NSArray , references returned by objectAtIndex: and other similar accessors are not guaranteed to stay valid when the parent container is deallocated. 当涉及到诸如NSArray类的集合类时,由objectAtIndex:和其他类似访问器返回的引用不能保证在释放父容器时保持有效。 In other words, objectAtIndex: does not return an autorelease'd object. 换句话说, objectAtIndex:不返回自动发布的对象。

This means the pointer you return will probably end up becoming invalid once the array it came from is deallocated. 这意味着一旦释放了来自其的数组,返回的指针可能最终将变得无效。

To fix this, use a retain + autorelease in your return statement: 要解决此问题,请在return语句中使用retain + autorelease

return [[view retain] autorelease];

UPDATE: 更新:

I'm unable to reproduce this warning in my version of Xcode. 我无法在我的Xcode版本中重现此警告。 But perhaps Martin is correct that the 'copy' is being interpreted incorrectly by the version of GCC/clang that you are using. 但是Martin可能是正确的,因为您使用的GCC / clang版本对“副本”的解释不正确。 This warning doesn't appear with the latest Xcode and gcc/clang compilers, and the rules are that only a prefix of "copy" or "mutableCopy" is interpreted as returning a +1 retained object. 最新的Xcode和gcc / clang编译器不会出现此警告,并且规则是仅将“ copy”或“ mutableCopy”的前缀解释为返回+1保留的对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM