
使用工具时,我发现了一个关于内存泄漏的奇怪问题。我的应用程序有一个日志机制,通过应用程序记录事件和与服务器的整个通信(请求-响应)。写入的每个事件对象都有一个时间戳。该时间戳的获取方式如下:
[NSDate descriptionWithLocale:[NSLocale systemLocale]]使用仪器,我发现descriptionWithLocale会导致泄漏。
代码如下:
-(id)initEvent:(NSString*)eventStr details:(NSString*)detailsStr{
eventString = [[NSMutableString alloc] initWithString:eventStr];
detailsString = [[NSString alloc] initWithString:detailsStr];
date =[[NSMutableString alloc] init];
NSDate* currentDate = [NSDate date];
NSString *dateDescStr = [currentDate descriptionWithLocale:[NSLocale systemLocale]];
[date appendString:dateDescStr];
return [super init];谢谢,亚历克斯。
发布于 2011-07-06 00:06:33
你确定是descriptionWithLocale导致你的内存泄漏吗?您没有使用属性访问器/赋值器来设置类属性,而是在dealloc方法之外的方法中释放它们,这将导致问题。至少,您在initEvent中分配eventString、detailsString和date (它是所有者,因为您没有使用属性访问器/赋值器)并在eventDictionary方法中释放它们,这违反了基本的内存管理规则。
我首先尝试以下方法(假设您的所有属性都具有retain属性):
-(id)initEvent:(NSString*)eventStr details:(NSString*)detailsStr{
this.eventString = [[NSMutableString alloc] initWithString:eventStr];
this.detailsString = [[NSString alloc] initWithString:detailsStr];
this.date =[[NSMutableString alloc] init];
NSDate* currentDate = [NSDate date];
NSString *dateDescStr = [currentDate descriptionWithLocale:[NSLocale systemLocale]];
[date appendString:dateDescStr];
[eventString release];
[detailsString release];
[date release];
return [super init];
}另外,根据标准内存管理模式,从eventDictionary中删除释放调用,并将它们添加到dealloc方法中。
试一试,看看它是否有帮助。如果我能澄清什么请告诉我。
发布于 2011-07-06 18:03:09
感谢您的解决方案。它工作得很好。事实上,descriptionWithLocale不是泄漏的原因,而是我自己的内存规则实现(是在1年前:( )。奇怪的是,仪器指向那个方法调用……看看我的类现在是什么样子:
@implementation EventItem
@synthesize eventString, detailsString, date;
-(id)initEvent:(NSString*)eventStr details:(NSString*)detailsStr{
if((self = [super init])){
self.eventString = [[NSMutableString alloc] initWithString:eventStr];
self.detailsString = [[NSString alloc] initWithString:detailsStr];
self.date =[[NSMutableString alloc] init];
NSDate* currentDate = [NSDate date];
NSString *dateDescStr = [currentDate descriptionWithLocale:[NSLocale systemLocale]];
[date appendString:dateDescStr];
[eventString release];
[detailsString release];
[date release];
}
return self;
}
-(NSMutableDictionary*)eventDictionary{
if(!dictionary)
dictionary = [[NSMutableDictionary alloc] init];
else
return dictionary;
[dictionary setObject:self.date forKey:@"Event"];
[dictionary setObject:self.date forKey:@"Date"];
[dictionary setObject:self.detailsString forKey:@"Details"];
[dictionary setObject:self.eventString forKey:@"EventDescription"];
return [dictionary autorelease];
}
-(void)dealloc{
// NSLog(@"dealloc called in EventItem");
[eventString release];
[detailsString release];
[date release];
[super dealloc];
}
@end现在,我遇到的另一个问题是关于通过"loadWithContentsOfFile:“加载的一些图像:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = OC_CELL_HEIGHT;
self.tableView.backgroundColor = [UIColor clearColor];
UIImage* backgroundImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TableBg" ofType:@"png"]];
UIImageView* background = [[UIImageView alloc] initWithFrame:self.tableView.frame];
background.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
background.image = backgroundImage;
[background sizeToFit];
[backgroundImage release];
[self.tableView setBackgroundView:background];
[background release];
[self.tableView setAutoresizesSubviews:YES];
selectedOCData = nil;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
}这行代码:
UIImage* backgroundImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TableBg" ofType:@"png"]];泄漏了98%,最大的回溯是240kb。我附上了一个带有仪器的截图。有没有问题不是initWithContentsOfFile的实际调用,而是tableView没有被正确释放?(我的类是一个tableViewController)。
谢谢,亚历克斯。

https://stackoverflow.com/questions/6585286
复制相似问题