开发者

[NSDate descriptionWithLocale:[NSLocale systemLocale]] leak?

[NSDate descriptionWithLocale:[NSLocale systemLocale]] leak?

Using instruments I found a strange problem regarding memory leaks. My app has a log mechanism which record events through the app and the whole communication with the server(request-response). Each event object that is been wrote has a timestamp. This timestamp is get as follows:

[NSDate descriptionWithLocale:[NSLocale systemLocale]]

Using instruments, I saw that descriptionWithLocale cause leaks.

Below is the code:

-(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];

Thanks, Alex.


Are you sure it's descriptionWithLocale that's causing your memory leak? You're not using the property accessor/mutator to set your class properties and you're releasing them in a method other than your dealloc method, which is going to cause issues. At the least, you're violating the basic memory management rules by allocating eventString, detailsString, and date in initEvent (which is then the owner, because you don't use a property accessor/mutator) and releasing them in the eventDictionary method, which is not the owner.

I would start by trying the following (assuming all your properties have the retain attribute):

-(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];
}

Also remove your release calls from eventDictionary and add them to the dealloc method, as per the standard memory management pattern.

Give that a try and see if it helps. Let me know if I can clarify anything.


Thanks for your solution. It works great. Indeed not descriptionWithLocale was the cause of the leak, but my own memory rule implementation (was 1 year ago :( ). Strange that instruments was pointing out to that method call.... See how my class looks now:

@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

Now, another problem that i have is regarding to some image loaded via "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;
}

The line :

 UIImage* backgroundImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TableBg" ofType:@"png"]];

leaks 98% and the haviest backtrace is 240 kb. I attached a screenshot with instruments for this. Is there a problem not with the actual call of initWithContentsOfFile, but with the tableView which is not deallocated correctly?(My class is a tableViewController).

Thanks, Alex.

[NSDate descriptionWithLocale:[NSLocale systemLocale]] leak?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜