Необходимо вызвать retain для свойства с указанным флагом retain - PullRequest
1 голос
/ 12 июня 2011

Я новичок в Objective-C, и, как и у большинства новичков, у меня есть вопросы об управлении ссылками.

Я написал класс, который загружает данные, используя NSURLConnection.Код аналогичен примеру Apple в http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html. Единственное отличие состоит в том, что переменная receiveData объявлена ​​как "@property (nonatomic, retain) NSMutableData * receiveData;"В файле .m у меня есть "@synthesize receiveData = _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

Если я изменяю назначение receiveData на этот код:

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

Тогда программа работает правильно и утечки памяти не обнаружены.

Как видите, мне нужно вызвать retain для NSMutableData, и я использую свойство, которое объявлено как "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 ]

3 голосов
/ 12 июня 2011

Не звоните [self.receivedData release] - это оставляет внутренний указатель висят. Весь смысл сохраненного свойства состоит в том, что оно освобождает себя. Просто сделай self.receivedData = nil.

1 голос
/ 12 июня 2011

Вот ваша проблема:

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

Вы освобождаете атрибут дважды (первый раз явно и второй неявно, присваивая ноль).

...