本文介绍了调用者此时不拥有的对象的引用计数的不正确的减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iPhone上的调用者此时不拥有的对象的引用计数的不正确递减。它发生在NSString,我明确地在for循环中初始化和释放。我试图像autoreleases字符串一样做,但我得到了泄漏。我认为罪魁祸首是stringbytrimming调用。任何建议,顺便说一下,这不会泄漏,但我在构建和分析中得到警告。一切都运行良好,应用程序不会崩溃。

Incorrect decrement of the reference count of an object that is not owned at this point by the caller on iPhone. It is happening with NSString which I clearly init and release within the for loop. I have tried to do the same as an autoreleases string but I get leaks. I assume the culprit is the stringbytrimming call. Any suggestions, by the way this does not leak, but I get the warning in build and analyze. Everything also works fine and the app does not crash.

for(int i=0;i<storyQuantity;i++) {
            NSString *imageString = [[NSString alloc] init];
            imageString = [[[storiesArray objectAtIndex:i] objectForKey: @"image"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  // must add trimming to remove characters

            imageLoader *imageOperation = [[imageLoader alloc] initWithImageURL:imageString target:self action:@selector(didImageLoad:) number:i];

            AppDelegate_iPad *appDelegate = [[UIApplication sharedApplication] delegate];
            [appDelegate.queue_ addOperation:imageOperation];
            [imageOperation release];
            [imageString release];
        }

UPDATE - 添加了我的imageLoader类,据我所知,这不是有泄漏

UPDATE - added my imageLoader class, which to the best of my knowledge does not have a leak

- (id)initWithImageURL:(NSString *)url target:(id)target action:(SEL)action number:(int)number {
    if(self = [super init]) {
        _action = action;
        _target = target;
        _number = number;
        if(url == nil) {
            return nil;
        } else {
            _imgURL = [[NSURL alloc] initWithString:[url copy]];
        }
    }
    return self;
}

- (id)main {

    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    if ([self isCancelled]) {
        NSLog(@"OPERATION CANCELLED");
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [pool drain];
        return nil;
    } else {

        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

        NSData *imgData = [[NSData alloc] initWithContentsOfURL:_imgURL];
        UIImage *image = [[UIImage alloc] initWithData:imgData];
        [imgData release];

        if ([self isCancelled]) {
            NSLog(@"OPERATION CANCELLED");
            [image release];
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            [pool drain];
            return nil;
        } else { 

            NSNumber *tempNumber = [NSNumber numberWithInt:_number];
            NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:tempNumber, @"number", image, @"image", nil];
            [image release];

            if([_target respondsToSelector:_action])
                [_target performSelectorOnMainThread:_action withObject:tempDict waitUntilDone:NO];
        }
    }

    [pool drain];
    return nil;

}

- (void)dealloc {
    [_imgURL release];
    [super dealloc];
}


推荐答案

因为你正在重新分配 imageString 变量,对原始对象的引用将丢失。为什么还要分配一个空字符串?只需将代码更改为

Since you are reassigning the imageString variable, the reference to the original object is lost. Why allocate an empty string anyway? Just change the code to

NSString *imageString = [[[storiesArray objectAtIndex:i] objectForKey: @"image"]
   stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

并删除 [imageString release] 和你很高兴。

这篇关于调用者此时不拥有的对象的引用计数的不正确的减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:13