【问题标题】:Why does using my custom class cause a memory error?为什么使用我的自定义类会导致内存错误?
【发布时间】:2011-06-08 06:25:11
【问题描述】:

我得到以下与下面的解析器方法相对应的内存泄漏错误。也就是说,每次我使用自定义类的 setter(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;

    }


}

【问题讨论】:

标签: iphone ios memory-leaks


【解决方案1】:

您必须实现dealloc 方法,您将在其中释放复制的字符串。

- (void) dealloc
{
    [super dealloc];
    [contactID release];
    [name release];
    [fax release];
    [company release];
    [phone release];
}

【讨论】:

    猜你喜欢
    • 2020-06-16
    • 2022-01-15
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多