【问题标题】:Not able to resolve a leak detected by leaks tool. Can i ignore it?无法解决泄漏工具检测到的泄漏。我可以忽略它吗?
【发布时间】:2011-09-22 14:32:03
【问题描述】:

我无法找到消除泄漏工具检测到的泄漏的方法。 这是我的问题... 我的委托中有一个单例对象,它在全局级别存储数据。现在,我有一个对象数组,我在这里维护并从控制器添加或修改它。 下面是一个填充对象并设置上述全局数组的函数, 现在,突出显示的行(由 //LEAK 标记)是泄漏工具告诉我它的泄漏的地方。我的会话需要这个数组。当我注销时,我会在最后释放数组。 我应该担心这种泄漏吗?

-(LayoutInfo *) fillLayout: (GDataXMLElement *) layoutElement {

        LayoutInfo *layout = [[LayoutInfo alloc] init];
        layout.dataTableCount = 0;
        layout.chartsCount = 0;
        NSArray *templateNameArr = [layoutElement elementsForName:@"TemplateName"];


        NSMutableArray *chartElements = [[NSMutableArray alloc] init];   // LEAK 
        NSMutableArray *dtElements = [[NSMutableArray alloc] init];
        NSArray *charts = [layoutElement elementsForName:@"chart"];     // LEAK 
        if (charts.count > 0) {
            for (GDataXMLElement *singleChart in charts) {
                chart *chartInfo = [[chart alloc] init];  // LEAK 
                layout.chartsCount = layout.chartsCount + 1;
                NSArray *imageName = [singleChart elementsForName:@"imageName"];
                if (imageName.count > 0) {
                    GDataXMLElement *imageNameStr = (GDataXMLElement *) [imageName objectAtIndex:0];
                    chartInfo.imageName = imageNameStr.stringValue; // LEAK 
                }           
                NSArray *imagePath = [singleChart elementsForName:@"imagePath"];
                if (imagePath.count > 0) {
                    GDataXMLElement *imagePathStr = (GDataXMLElement *) [imagePath objectAtIndex:0];
                    chartInfo.imagePath = imagePathStr.stringValue;  // LEAK 
                } 

                NSArray *imageFileName = [singleChart elementsForName:@"imageFileName"];
                if (imageFileName.count > 0) {
                    GDataXMLElement *imageFileNameStr = (GDataXMLElement *) [imageFileName objectAtIndex:0];
                    chartInfo.imageFileName = imageFileNameStr.stringValue;
                } 
                ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:chartInfo.imagePath]];
                [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] 
                                                     stringByAppendingPathComponent:chartInfo.imageFileName]];

                [request setDidFinishSelector:@selector(fillLayout_requestDone:)];
                [request setDidFailSelector:@selector(fillLayout_requestWentWrong:)];
                [request startSynchronous];

                NSString *imagePath1 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:chartInfo.imageFileName];
                if([[NSFileManager defaultManager] fileExistsAtPath:imagePath1]) {
                    NSLog(@" --- IMAGE SAVED -- %@", imagePath1);
                }
                [chartElements addObject:chartInfo];
            } //for
            layout.chartElement = chartElements;   // THIS IS WHERE I ASSIGN THE GLOBAL ARRAY
            //[chartElements release];
        } 
        return layout;
    }

【问题讨论】:

  • chartElement 是如何在LayoutInfo 中声明的?
  • layout.chartElement - 布局中的chartElement 是什么属性?如果是retaincopy,你应该释放chartElements(注释行)。如果是assign,则必须释放layout.chartElements(尽管如果您也从其他地方分配它,这也是不安全的)。 dtElements 似乎没有被使用并且泄漏,要么删除包含它的行,或者如果你想使用它记得在之后释放它。
  • @property(assign)NSMutableArray *chartElement;
  • 我已经发布了 dtElements。谢谢。但同样,我将在我的会话中使用 layout.chartElement。因为我可能需要它,所以现在发布它并没有帮助。我可以在提交时忽略此泄漏吗?这是我现在唯一的泄漏。
  • 让chartElements成为retain属性,在layout.chartElements = chartElements之后释放局部变量。 release 并不意味着对象被删除。这只是意味着一个所有者已经完成了它。当所有所有者都使用它时,它将被删除。还要在你的 dealloc 中释放 chartElements(虽然如果它是一个单例,它可能永远不会被调用,但我认为泄漏不会抱怨这一点)。

标签: iphone ios memory-leaks objective-c++


【解决方案1】:
-(LayoutInfo *) fillLayout: (GDataXMLElement *) layoutElement {

    LayoutInfo *layout = [[LayoutInfo alloc] init];
    layout.dataTableCount = 0;
    layout.chartsCount = 0;
    NSArray *templateNameArr = [layoutElement elementsForName:@"TemplateName"];


    NSMutableArray *chartElements = [[NSMutableArray alloc] init];   // LEAK 
    //NSMutableArray *dtElements = [[NSMutableArray alloc] init];
    NSArray *charts = [layoutElement elementsForName:@"chart"];     // LEAK 
    if (charts.count > 0) {
        for (GDataXMLElement *singleChart in charts) {
            chart *chartInfo = [[chart alloc] init];  // LEAK 
            layout.chartsCount = layout.chartsCount + 1;
            NSArray *imageName = [singleChart elementsForName:@"imageName"];
            if (imageName.count > 0) {
                GDataXMLElement *imageNameStr = (GDataXMLElement *) [imageName objectAtIndex:0];
                chartInfo.imageName = imageNameStr.stringValue; // LEAK 
            }           
            NSArray *imagePath = [singleChart elementsForName:@"imagePath"];
            if (imagePath.count > 0) {
                GDataXMLElement *imagePathStr = (GDataXMLElement *) [imagePath objectAtIndex:0];
                chartInfo.imagePath = imagePathStr.stringValue;  // LEAK 
            } 

            NSArray *imageFileName = [singleChart elementsForName:@"imageFileName"];
            if (imageFileName.count > 0) {
                GDataXMLElement *imageFileNameStr = (GDataXMLElement *) [imageFileName objectAtIndex:0];
                chartInfo.imageFileName = imageFileNameStr.stringValue;
            } 
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:chartInfo.imagePath]];
            [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] 
                                                 stringByAppendingPathComponent:chartInfo.imageFileName]];

            [request setDidFinishSelector:@selector(fillLayout_requestDone:)];
            [request setDidFailSelector:@selector(fillLayout_requestWentWrong:)];
            [request startSynchronous];

            NSString *imagePath1 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:chartInfo.imageFileName];
            if([[NSFileManager defaultManager] fileExistsAtPath:imagePath1]) {
                NSLog(@" --- IMAGE SAVED -- %@", imagePath1);
            }
            [chartElements addObject:chartInfo];
            [chartInfo release];                   // it's retained in chartElements until removed, or until chartElements is deallocced
        } //for
        if(layout.charElement){
            [layout.charElement release];     // you should however consider in making charElement property as retain;
            layout.charElement = nil;         // this isn't required here (since you're assigning it a new value), but you should usually set it to nil after a release to prevent EXC_BADACCESS
        }
        layout.chartElement = chartElements;   // THIS IS WHERE I ASSIGN THE GLOBAL ARRAY
        //[chartElements release];
    } 
    return [layout autorelease];   // in case you don't want it autoreleased you should call your method something like: createFilledLayout ('create' is usually used so anyone that uses the method knows it's responsible for releasing the return value)
}

你应该看看Memory Management Programming Guide

【讨论】:

  • 是的,我想我应该这样做。
猜你喜欢
  • 2015-08-04
  • 2012-09-21
  • 1970-01-01
  • 2012-07-16
  • 2018-09-25
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
相关资源
最近更新 更多