相关疑难解决方法(0)

Objective-C中的递归块在ARC中泄漏

所以我正在使用递归块.我理解,对于要递归的块,它需要以__block关键字开头,并且必须将其复制,以便将其放在堆上.但是,当我这样做时,它显示为仪器中的泄漏.有谁知道为什么或如何绕过它?

请注意,在下面的代码中,我已经引用了许多其他块,但它们都不是递归的.

__block NSDecimalNumber *(^ProcessElementStack)(LinkedList *, NSString *) = [^NSDecimalNumber *(LinkedList *cformula, NSString *function){
        LinkedList *list = [[LinkedList alloc] init];
        NSDictionary *dict;
        FormulaType type;
        while (cformula.count > 0) {
            dict = cformula.pop;
            type = [[dict objectForKey:@"type"] intValue];
            if (type == formulaOperandOpenParen || type == formulaListOperand || type == formulaOpenParen) [list add:ProcessElementStack(cformula, [dict objectForKey:@"name"])];
            else if (type == formulaField || type == formulaConstant) [list add:NumberForDict(dict)];
            else if (type == formulaOperand) [list add:[dict objectForKey:@"name"]];
            else if (type == formulaCloseParen) {
                if (function){ …
Run Code Online (Sandbox Code Playgroud)

objective-c instruments objective-c-blocks automatic-ref-counting

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

使用NSURLSession.downloadTaskWithURL时内存泄漏

所以我在与Swift的努力中遇到了另一个障碍.我正在尝试将多个图像加载到图像库中 - 除了一件事以外,一切正常.尽管我清除了图像,但应用程序的内存使用仍在不断增长和增长.在基本上消除了所有代码之后,我发现这是由我的图像加载脚本引起的:

func loadImageWithIndex(index: Int) {
    let imageURL = promotions[index].imageURL
    let url = NSURL(string: imageURL)!
    let urlSession = NSURLSession.sharedSession()
    let query = urlSession.downloadTaskWithURL(url, completionHandler: { location, response, error -> Void in

    })
    query.resume()
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,这段代码基本上没有任何功能.然而,每次我调用它,我的应用程序内存使用量都在增长.如果我注释掉查询,则内存使用量不会改变.

我已经阅读了几个类似的问题,但他们都涉及使用委托.那么,在这种情况下,没有委托,但存在内存问题.有人知道如何消除它以及导致它的原因吗?

编辑:这是一个完整的测试类.似乎只有在可以加载图像时内存才会增长,就像指向图像的指针永远保存在内存中一样.当找不到图像时,没有任何反应,内存使用率保持低水平.也许有些暗示如何清理这些指针?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        //memory usage: approx. 23MB with 1 load according to the debug navigator
        //loadImage()

        //memory usage approx 130MB with the cycle below according to the debug navigator
        for i in 1...50 {
            loadImage() …
Run Code Online (Sandbox Code Playgroud)

memory memory-leaks ios nsurlsession swift

13
推荐指数
2
解决办法
8596
查看次数