BlogEntry上的内存泄漏在哪里?

时间:2012-03-25 15:15:49

标签: objective-c ios cocoa-touch memory-leaks

我正在分析iPhone的iOS 4应用程序,我得到了这个日志:

Leaked Object   #   Address Size    Responsible Library Responsible Frame
BlogEntry,9 < multiple >    288 Bytes   Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a11700   32 Bytes    Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a33e30   32 Bytes    Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a44b80   32 Bytes    Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a47950   32 Bytes    Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a4b510   32 Bytes    Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a5e840   32 Bytes    Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a5e8c0   32 Bytes    Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a647c0   32 Bytes    Paula   -[BlogViewController dataReceived]
 BlogEntry,1    0x9a74ee0   32 Bytes    Paula   -[BlogViewController dataReceived]

BlogViewController dataReceived方法就是这个:

- (void) dataReceived
{
    NSArray* data = [conn.parsedData objectForKey:kRootKey];

    blogEntries = [[NSMutableArray alloc] initWithCapacity:data.count];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone *timezone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [df setTimeZone:timezone];

    for (int i = 0; i < data.count; i++)
    {
        NSDictionary* object = [data objectAtIndex:i];

        NSString* titulo = [object objectForKey:kTituloKey];
        NSString* texto = [object objectForKey:kTextoKey];
        NSDate* fecha = [df dateFromString: [object objectForKey:kFechaKey]];
        NSString* foto = [NSString stringWithFormat:@"%@%@", kPhotoURLPrefix, [object objectForKey:kFotoKey]];

        BlogEntry* blogE = [[BlogEntry alloc] initWithTitle:titulo
                                                       text:texto
                                                       date:fecha
                                                      photo:foto];
        [blogEntries addObject:blogE];

        [blogE release];
    }

    [df release];
    [blogList reloadData];

    loadingView.hidden = YES;
}

但我认为问题出在BlogEntry类:

BlogEntry.h

@interface BlogEntry : NSObject
{
    NSString* title;
    NSString* text;
    NSDate* date;
    NSString* photo;
}

@property (nonatomic, readonly) NSString* title;
@property (nonatomic, readonly) NSString* text;
@property (nonatomic, readonly) NSString* photo;
@property (nonatomic, readonly) NSDate* date;

- (id)initWithTitle:(NSString*)titulo
               text:(NSString*)texto
               date:(NSDate*)fecha
              photo:(NSString*)foto;

@end

BlogEntry.m

#import "BlogEntry.h"

@implementation BlogEntry

@synthesize title;
@synthesize text;
@synthesize date;
@synthesize photo;

- (id)initWithTitle:(NSString*)titulo
               text:(NSString*)texto
               date:(NSDate*)fecha
              photo:(NSString*)foto
{
    if (self = [super init])
    {
        title = [titulo copy];
        text = [texto copy];
        date = [fecha retain];
        photo = [foto copy];
    }
    return self;
}

- (void) dealloc
{
    [title release];
    [text release];
    [date release];
    [photo release];

    [super dealloc];
}

@end

你知道我的内存泄漏在哪里吗?我无法找到它。

2 个答案:

答案 0 :(得分:1)

您应该查看Static Analyzer。它会告诉你什么是错的。

请参阅本文档中的Find Coding Mistakes部分: http://developer.apple.com/library/mac/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/Debugging/Debugging.html

...或者只是自己尝试一下:在XCode中按Product > Analyze

答案 1 :(得分:0)

blogEntries = [[NSMutableArray alloc] initWithCapacity:data.count];