【问题标题】:ARC Converted Application Issue: passing adress of non local object to _autoreleaseing parameter for write-backARC 转换的应用程序问题:将非本地对象的地址传递给 _autoreleaseing 参数以进行回写
【发布时间】:2012-08-06 09:57:15
【问题描述】:

我正在尝试使用 ios5 制作应用程序,我真的很困惑为什么会发生这种情况。有人可以帮我解释一下为什么出现&error时会出现错误

将非本地对象的地址传递给__autoreleasing参数以进行回写

以下调用导致错误

// note the following method returns _inStream and _outStream with a retain count that the caller must eventually release

if (![netService getInputStream:&_inStream outputStream:&_outStream]) {

 NSLog(@"error in get input and output streams");
 return;
}

我的班级.h

NSInputStream      *_inStream;
NSOutputStream     *_outStream;

@property (nonatomic, strong) NSInputStream *_inStream;    
@property (nonatomic, strong) NSOutputStream *_outStream;

我的班级.m

@synthesize _inStream;
@synthesize _outStream;

getInputStream是一个NSNetService类方法

请在下面的NSNetService类getInputStream实现

/* Retrieves streams from the NSNetService instance. The instance's delegate methods are not called. Returns YES if the streams requested are created successfully. Returns NO if or any reason the stream could not be created. If only one stream is desired, pass NULL for the address of the other stream. The streams that are created are not open, and are not scheduled in any run loop for any mode.
*/
- (BOOL)getInputStream:(NSInputStream **)inputStream outputStream:(NSOutputStream **)outputStream;

我在

中找到了相关帖子

non-local-object-to-autoreleasing-parameter

-pointer-ownership-issues

non-local-object-to-autoreleasing

from-ios-4-to-ios-5arc-passing-address

但是我找不到问题

感谢您对此问题的任何帮助

谢谢

【问题讨论】:

    标签: iphone objective-c automatic-ref-counting


    【解决方案1】:

    我找到了解决办法

    我们开始吧。将变量创建为本地变量并将它们分配回原始变量。感谢@borreden 提供的有见地的信息。

    这是我最新的代码。

    NSInputStream  *tempInput  = nil;
    NSOutputStream *tempOutput = nil;
    
    // note the following method returns _inStream and _outStream with a retain count that the caller must eventually release
    if (![netService getInputStream:&tempInput outputStream:&tempOutput]) {
    
        NSLog(@"error in get input and output streams");
        return;
    }
    _inStream  = tempInput;
    _outStream = tempOutput;
    

    【讨论】:

      【解决方案2】:

      原因是你在使用ARC时不能像这样传递你的_inStream_outStream。 (因为您在转换后的版本中使用了 ARC)

      因为如果这些变量在方法中被覆盖,它们就会泄漏。

      你需要改变你处理的返回值(如果它改变了)或者只是作为一个普通参数传递。

      没有明确的内存管理与保留/释放,因此 ARC 无法自动处理这种情况。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        • 2012-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多