Почему использование моего пользовательского класса вызывает ошибку памяти? - PullRequest
0 голосов
/ 08 июня 2011

Я получаю следующие ошибки утечки памяти, которые соответствуют методу парсера ниже.То есть каждый раз, когда я использую установщик моего пользовательского класса (4 раза), я получаю утечку памяти:

Leaked Object   #   Address Size    Responsible Library Responsible Frame
NSCFString  630 < multiple >    20160   Foundation  -[NSCFString copyWithZone:]
NSCFString  625 < multiple >    10000   Foundation  -[NSCFString copyWithZone:]
NSCFString  623 < multiple >    19936   Foundation  -[NSCFString copyWithZone:]
NSCFString  613 < multiple >    23664   Foundation  -[NSCFString copyWithZone:]

мой интерфейс пользовательского класса:

#import <Foundation/Foundation.h>

@interface FaxRecipient : NSObject {
    NSString * contactID;
    NSString * name;
    NSString * fax;
    NSString * company;
    NSString * phone;
}
@property(nonatomic,copy)NSString *contactID;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *fax;
@property(nonatomic,copy)NSString *company;
@property(nonatomic,copy)NSString *phone;

@end

реализация:

#import "FaxRecipient.h"

@implementation FaxRecipient
@synthesize contactID, name, fax, company, phone;
@end

Я использую его в следующем контексте:

 -(void)parser:(NSXMLParser *)parser
    didStartElement:(NSString *) elementName
     namespaceURI:(NSString *)namespaceURI
    qualifiedName:(NSString *)qName
       attributes:(NSDictionary *)attributeDict
{

    if ([elementName isEqual:@"ContactId"]) {
        faxRecipient =[[FaxRecipient alloc]init];
        remoteRecipientString = [[NSMutableString alloc]init]; 
    }

    else if ([elementName isEqual:@"Name"]) {
        remoteRecipientString = [[NSMutableString alloc]init]; 


    }else if ([elementName isEqual:@"Fax"]) {
        remoteRecipientString = [[NSMutableString alloc]init];
    }

    else if ([elementName isEqual:@"Rec"]) {
        remoteRecipientString = [[NSMutableString alloc]init];
    }

}

-(void) parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string{

    [remoteRecipientString appendString:string];

}


-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *) elementName
 namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName{


    if ([elementName isEqual:@"ContactId"]) {
        faxRecipient.contactID = remoteRecipientString;
        [remoteRecipientString release];
        remoteRecipientString = nil;
    }


    if ([elementName isEqual:@"Name"]) {
        faxRecipient.name = remoteRecipientString;
        [remoteRecipientString release];
        remoteRecipientString = nil;
    }   


    if ([elementName isEqual:@"Fax"]) {
        faxRecipient.fax = remoteRecipientString;
        [remoteRecipientString release];
        remoteRecipientString = nil;

    }

    if ([elementName isEqual:@"Rec"]) {
        faxRecipient.phone = remoteRecipientString;
        [remoteRecipientItems addObject:faxRecipient];//delivery complete as this is end of xml
        [faxRecipient release];
        faxRecipient = nil;
        [remoteRecipientString release];
        remoteRecipientString = nil;

    }


}

1 Ответ

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

Вы должны реализовать метод dealloc, чтобы освободить скопированные строки.

- (void) dealloc
{
    [super dealloc];
    [contactID release];
    [name release];
    [fax release];
    [company release];
    [phone release];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...