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

发布于 2024-12-06 08:54:12 字数 3091 浏览 0 评论 0原文

我无法找到一种方法来消除泄漏工具检测到的泄漏。 这是我的问题... 我的委托中有一个单例对象,它在全局级别上存储数据。现在,我有一个对象数组,我在这里维护它们并从控制器添加或修改它。 下面是一个填充对象并设置上述全局数组的函数, 现在,突出显示的行(由 //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;
    }

I am not able to find a way to remove a leak detected by leaks tool.
Here is my problem...
I have a singleton object in my delegate which stores data on a global level. Now, I have a array of objects which i maintain here and add or modify it from controllers.
Below is a function which fills the objects and sets the above global array,
Now, the highlighted lines(marked by //LEAK) are where the leaks tool tell me its a leak. I require this array for my session. I release the array at the end when i logout.
Should i be worried about this kind 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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-12-13 08:54:12
-(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)
}

你应该看看内存管理编程指南

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

you should have a look at Memory Management Programming Guide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文