相关疑难解决方法(0)

在这个区块中强烈捕获自我可能会导致保留周期

如何在xcode中避免此警告.这是代码片段:

[player(AVPlayer object) addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.1, 100)
queue:nil usingBlock:^(CMTime time) {
    current+=1;

    if(current==60)
    {
        min+=(current/60);
        current = 0;
    }

    [timerDisp(UILabel) setText:[NSString stringWithFormat:@"%02d:%02d",min,current]];///warning occurs in this line
}];
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c retain avplayer automatic-ref-counting

205
推荐指数
5
解决办法
8万
查看次数

__weak和__block引用有什么区别?

我正在阅读Xcode的文档,这里有些令我困惑的事情:

__block typeof(self) tmpSelf = self;
[self methodThatTakesABlock:^ {
    [tmpSelf doSomething];
}];
Run Code Online (Sandbox Code Playgroud)

从文档中复制以下内容:

块形成对其捕获的变量的强引用.如果self在块中使用 ,则块形成强引用self,因此如果 self还具有对块的强引用(通常它具有),则会产生强引用循环.要避免循环,您需要__block在块外部创建一个弱(或)引用,如上例所示.

我不明白'弱(或__block)'是什么意思?

__block typeof(self) tmpSelf = self;
Run Code Online (Sandbox Code Playgroud)

__weak typeof(self) tmpSelf = self;
Run Code Online (Sandbox Code Playgroud)

这里完全一样吗?

我在文件中找到了另一篇文章:

注意:在垃圾收集环境中,如果将两个__weak__block修饰符都应用于变量,则该块将无法确保它保持活动状态.

所以,我完全不解.

memory-management weak-references objective-c ownership objective-c-blocks

77
推荐指数
2
解决办法
3万
查看次数

ASIHTTPRequest/ASIFormDataRequest - 在ARC下的块内引用请求对象

此问题非常相似,我正在尝试将使用ASIHTTPRequest&的项目转换ASIFormDataRequest为ARC.

在我的视图控制器类中,我经常request在完成块中引用和使用对象的属性(查看响应代码,响应数据等):

__block  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:SOME_URL]];    
[request setCompletionBlock:^{   

    if([request responseStatusCode] == 200) ....etc
Run Code Online (Sandbox Code Playgroud)

转换为ARC时,我收到警告:

在此块中强烈捕获"请求"可能会导致保留周期

这样做的正确方法是什么?

另一个SO用户在前一个帖子中指出,简单添加__weak可能会导致请求在块完成之前被释放,我认为这是真的.

如何在ARC下的完成/故障块中正确引用这些属性?

iphone objective-c asihttprequest ios automatic-ref-counting

21
推荐指数
1
解决办法
4914
查看次数

可以使用__weak属性将参数传递给块导致内存泄漏吗?

在我支持iOS ARC的代码中,我需要将"self"和其他对象传递给一个块.更具体地讲,我需要与自我和互动ASIHTTPRequest内部对象ASIHTTPRequestcompletionBlock.

_operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(parseServerReply) object:nil];
_request = [ASIHTTPRequest requestWithURL:@"server.address"];

// ...

[_request setCompletionBlock:^{
    [self setResponseString:_request.responseString];
    [[MyAppDelegate getQueue] addOperation:_operation];
}];
Run Code Online (Sandbox Code Playgroud)

为了避免以下警告:Capturing "self" strongly in this block will likely lead to a retain cycle.我已经修改了我的代码,以便__weak在此帖子之后添加属性块中使用的对象:修复警告"在此块中强烈捕获[一个对象]可能会导致保留周期"在启用ARC的代码中

结果代码是:

_operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(parseServerReply) object:nil];
_request = [ASIHTTPRequest requestWithURL:@"server.address"];

// ...
__weak NSOperation * operation = _operation;
__weak ASIHTTPRequest * request = _request;
__weak typeof(self) * self_ = self;

[request …
Run Code Online (Sandbox Code Playgroud)

iphone ios objective-c-blocks ios5

4
推荐指数
1
解决办法
3447
查看次数

警告:在此块中强烈捕获$ XXX可能会导致'AFHTTPClient.m'中的保留周期

AFNetworking在我的项目中使用,但它有2个警告: 警告

完整的警告字符串是:

/AFNetworking/AFHTTPClient.m:575:38: Capturing 'operation' strongly in this block is likely to lead to a retain cycle

warnings objective-c automatic-ref-counting afnetworking

2
推荐指数
1
解决办法
2651
查看次数

这样安全吗?单身作为自我在块中的可能保留周期

我很确定这是100%安全的,但我不想错过任何东西.我有以下代码

- (void) scheduleControlSurfaceProcess {
    [self.operationQueueForMessageProcessing addOperationWithBlock:^{
        // do something
        [self scheduleControlSurfaceProcess];     
    }];
}
Run Code Online (Sandbox Code Playgroud)

自我是单身人士.该块作为非主线程线程非常出色.我没有在profiler中看到任何内存问题(我不太相信).

那么,我可以忽略这个警告,"Block会被捕获对象强烈保留的对象保留吗?" 如果没有,我怎么能坚持要释放块(用ARC)?让警告消失很容易,但分配id what = self似乎无法解决问题.

编辑:正如我在这个问题中意识到的那么晚,这里真正的问题是我从块内部重新安排.这显然是有问题的,因为每个块保留下一个.

注意:我知道关于这个主题很多问题,但我不够专业,不知道哪个情况与此类似.

nsoperationqueue ios objective-c-blocks automatic-ref-counting

1
推荐指数
1
解决办法
1191
查看次数