我正在分析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


你知道我的内存泄漏在哪里吗?我找不到

最佳答案

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

关于objective-c - BlogEntry的内存泄漏在哪里?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9861256/

10-11 05:31