需要为具有指定的retain标志的属性调用retain

时间:2011-06-12 09:55:25

标签: iphone objective-c properties retain

我是Objective-C的新手,而且对于大多数新手我都有关于参考管理的问题。

我写了一个使用NSURLConnection下载数据的类。代码类似于Apple http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html中的示例。唯一的区别是receivedData变量被声明为“@property(nonatomic,retain)NSMutableData * receivedData;”在.m文件中,我有“@synthesize receivedData = _receivedData;”。

我有connectionStart函数,它开始下载数据。在这个函数中,我有这个代码:

if (theConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    self.receivedData = [NSMutableData data];
} else {
    // Inform the user that the connection failed.
}

程序因此消息崩溃:

2011-06-12 12:47:22.298 WebGallery[1212:207] *** -[NSConcreteMutableData release]: message sent to deallocated instance 0x118a6fe0

如果我将receivedData赋值更改为此代码:

self.receivedData = [[NSMutableData data] retain];

然后程序正常工作,没有检测到内存泄漏。

如您所见,我需要在NSMutableData上调用retain,并且我正在使用属性,该属性被声明为“retain”。

为什么会这样?

编辑:.m文件的完整内容:

#import "GalleryData.h"
#import "XmlParser.h"

@implementation GalleryData

@synthesize receivedData = _receivedData;
@synthesize imagesData = _imagesData;
@synthesize delegate = _delegate;
@synthesize currentObjectType = _currentObjectType;
@synthesize currentObjectIndex = _currentObjectIndex;

- (id) init
{
    [super init];
    _imagesData = [[NSMutableArray alloc] init];    
    return self;
}


- (void) dealloc
{
     [_imagesData release];
    _imagesData = nil;
    [super dealloc];
}

- (void) connectionStart:(NSURL *)theURL
{
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        //ASK: Kodėl čia reikia daryti retain jei @property jau nustatyta retain?
        self.receivedData = [[NSMutableData data] retain];
    } else {
        // Inform the user that the connection failed.
    }

}

- (void) startLoading
{
    NSLog(@"Loading started");

    self.currentObjectIndex = 0;
    self.currentObjectType = ObjectTypeXML;
    [self connectionStart:[NSURL URLWithString:@"http://www.aleksandr.lt/gallery/data.xml"]];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];
    [self.receivedData release];

    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection release];

    if (self.currentObjectType == ObjectTypeXML) {
        NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:self.receivedData];        
        XmlParser *parser = [[XmlParser alloc] initXmlParser:self.imagesData];

        [nsXmlParser setDelegate:parser];
        [nsXmlParser parse];        
        [nsXmlParser release];
        [parser release];

        [self.receivedData release];
        self.receivedData = nil;

        if ([self.imagesData count]) {
            self.currentObjectIndex = 0;
            self.currentObjectType = ObjectTypeThumbImage;
            ImageData *firstImage = [self.imagesData objectAtIndex:0];
            NSURL *theURL = [NSURL URLWithString:firstImage.thumbImageURL];
            [self connectionStart:theURL];
        } else {
            [self.delegate loadingFinished];
            return;
        }
    } else if (self.currentObjectType == ObjectTypeThumbImage) {
        ImageData *currentImage;
        currentImage = [self.imagesData objectAtIndex:self.currentObjectIndex];

        UIImage *thumbImage = [[UIImage alloc] initWithData:self.receivedData];

        if (thumbImage == nil) {
            NSLog(@"image was not created");
        }

        [currentImage setThumbImageScaled:thumbImage];

        [thumbImage release];
        [self.receivedData release];
        self.receivedData = nil;

        if (self.currentObjectIndex == ([self.imagesData count] - 1)) {
            [self.delegate loadingFinished];
            return;
        }

        self.currentObjectIndex++;

        currentImage = [self.imagesData objectAtIndex:self.currentObjectIndex];
        NSLog(@"'%@'", currentImage.thumbImageURL);
        NSURL *theURL = [NSURL URLWithString:currentImage.thumbImageURL];
        [self connectionStart:theURL];
    }
}

@end

2 个答案:

答案 0 :(得分:3)

不要拨打[self.receivedData release] - 这会让内部指针悬空。保留财产的全部意义在于它自我释放。只需self.receivedData = nil

答案 1 :(得分:1)

这是你的问题:

    [self.receivedData release];
    self.receivedData = nil;

你要释放两次属性(第一次明确地,第二次通过指定nil隐式地)。