1

NSKeyedUnarchiver unarchiveObjectWithFile:アプリケーションデータを読み込むために使用しています。インストルメントでリークを使用して実行すると、次のようにするとリークが発生すると言われます。

{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *archivePath = [[NSString alloc]initWithFormat:@"%@/Config.archive", documentsDirectory];

    //Following line produces memory leak
    applicationConfig = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];

    [archivePath release];

    if( applicationConfig == nil )
    {
        applicationConfig = [[Config alloc]init];
    }
    else
    {
        [applicationConfig retain];
    }
}

この線:

applicationConfig = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];

32 バイトのメモリ リークが発生しています。applicationConfig はインスタンス変数です。私のinitWithCode関数は単純に次のことを行います:

- (id)initWithCoder:(NSCoder *)coder {
    if( self = [super init] )
    {
                //NSMutableArray
        accounts = [[coder decodeObjectForKey:@"Accounts"] retain];
        //Int
                activeAccount = [coder decodeIntForKey:@"ActiveAccount"];       
    }
    return self;
}

理由の任意のアイデア

applicationConfig = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];

リークを生成していますか?

4

1 に答える 1

2

私の推測では、メモリリークは次の行が原因であると思われます:

[applicationConfig retain];

またはこの行:

accounts = [[coder decodeObjectForKey:@"Accounts"] retain];

メモリは に割り当てられunarchiveObjectWithFile:いますが、オブジェクトの余分な保持によってリークが発生します。applicationConfig適切にリリースしていることを確認してください。

于 2009-09-03T13:21:56.570 に答える