iphone - 无法解决泄漏工具检测到的泄漏。我可以忽略它吗?

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

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

-(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;
    }

最佳答案

-(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

关于iphone - 无法解决泄漏工具检测到的泄漏。我可以忽略它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7516531/

相关文章:

iphone - 是否必须开发iPad应用程序?

iphone - 操作无法完成。 AVAudioRecorder iPhone SDK

php - 在 PHP 中访问 JSON 元素

c++ - XCode 未显示 C++ 程序中的泄漏

iphone - Uibutton touchupinside action 破坏 Storyboard应用程序中的程序

ios - 如何查找使用 AVCaptureVideoDataOutput 录制的视频的持续时间

iOS 7 UISearchBar 定制当它是第一响应者

java - 如何解决 GWT 中的内存泄漏?

ios - 覆盖对象……这样可以吗?

ios - 如何通过编码重启IOS应用程序?