在UITableView中设置单元格数据时内存泄漏

时间:2010-09-10 13:59:55

标签: iphone objective-c xcode

当使用内存泄漏工具运行我的iPhone应用程序代码时,它表示我在此方法中有两个内存泄漏。调用cell.textLabel setText时的第一个,调用cell.imageView时的第二个setImage:

我无法弄清楚这是什么问题,请你能帮助我吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"IssuesCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // The issue object
    Issue *issue;
    issue = [issues objectAtIndexPath:indexPath];

    // Issue name
    [cell.textLabel setText:[issue name]];

    // Get a string from the issue date
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    [cell.detailTextLabel setText:[dateFormatter stringFromDate:[issue date]]];
    [dateFormatter release];
    [cell.imageView setImage:[UIImage imageNamed:[issue cover]]];

    return cell;
}

问题定义:

#import <CoreData/CoreData.h>

@class Article;

@interface Issue :  NSManagedObject  
{
}

@property (nonatomic, retain) NSString * cover;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSSet* articles;

@end


@interface Issue (CoreDataGeneratedAccessors)
- (void)addArticlesObject:(Article *)value;
- (void)removeArticlesObject:(Article *)value;
- (void)addArticles:(NSSet *)value;
- (void)removeArticles:(NSSet *)value;

@end

实施

#import "Issue.h"

#import "Article.h"

@implementation Issue 

@dynamic cover;
@dynamic name;
@dynamic date;
@dynamic articles;

@end

1 个答案:

答案 0 :(得分:0)

鉴于两个已识别的行都包含对'issue'方法的调用,并且正如其他人已经指出的那样,在您发布的代码中没有明显的泄漏,因此这两种方法可能存在问题。您的代码假定它们要么不返回带有必须释放的引用的对象。