1

ファイル/ディレクトリのアイコン画像を見つける必要があるココアアプリケーションを作成しています。指定されたパスからアイコン画像を取得するためにこれを使用しています。

NSImage *image = [[NSWorkspace sharedWorkspace] iconForFile:path];

このメソッドは、アプリケーション全体で劣勢的に呼び出されます。アプリを使用するほど、アプリケーションの割り当てメモリが増加します。メモリリークを探すときにインストゥルメントを使用すると、上記の方法が100%のメモリリークの原因であることがわかりました。このメモリリークをどのように削除できますか、またはアイコン画像とメモリを取得できる他の方法は問題になりません。誰か助けてください。ありがとう

編集:

これは、このメソッドを呼び出す元のメソッドです。

-(WEFile *)fileAtPath:(NSString *)filePath
{

    WEFile *file                = [[WEFile alloc] init];
    file.fIconImage             = [workSpace iconForFile:filePath];

    file.Name                   = [fileManager displayNameAtPath:filePath];
    file.type                   = [workSpace localizedDescriptionForType:[workSpace typeOfFile:filePath error:&error]];

    NSDictionary *detailDict    = [fileManager attributesOfItemAtPath:filePath error:&error];
    file.modificationDate       = [ Utility createDateFormat:[detailDict objectForKey:NSFileModificationDate]];
    file.creationDate           = [ Utility createDateFormat:[detailDict objectForKey:NSFileCreationDate]];
    file.Size                   = [[detailDict objectForKey:NSFileSize] integerValue];
    file.fPath                  = filePath;

    NSDictionary *metaDict      = [self metadataForFileAtPath:filePath];
    file.addedDate              = [ Utility createDateFormat:[metaDict objectForKey:@"kMDItemDateAdded"]];
    file.lastOpenedDate         = [ Utility createDateFormat:[metaDict objectForKey:@"kMDItemLastUsedDate"]];  
    return [file autorelease];

}

メソッド fileAtPath が別のメソッドから再帰的に呼び出され、WEFile クラス オブジェクトが配列に格納されます。テーブルビューで表示します。

編集 2: これは、fileAtPath メソッドを呼び出すコードです。そして、このメソッド directoryAtPath は、ディレクトリのパスをパラメーターとしてテーブル選択を渡すときに呼び出されます。

-(WEDirectory *)directoryAtPath:(NSString *)dirPath
{

    WEDirectory *dir      = [[[WEDirectory alloc] init] autorelease];

    NSArray *childArray = [self getContentsWithinDirectory:dirPath];

    if (childArray && [childArray count]>0)
    {
      for (int i = 0; i<[childArray count]; i++)
      {

         NSURL *fileURL     = [childArray objectAtIndex:i];
         NSString *filePath = [self getPathFromURL:fileURL];
         filePath    = [filePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

         BOOL isDir;

         BOOL success = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
         if (!success)
         {
               continue;
         }

        if(isDir)
        {

           WEDirectory *childDir       = [[WEDirectory alloc] init] ;
           childDir.dIconImage         = [workSpace iconForFile:filePath];

           childDir.Name               = [fileManager displayNameAtPath:filePath];
           childDir.type               = [workSpace localizedDescriptionForType:[workSpace typeOfFile:dirPath error:&error]];


           NSDictionary *detailDict    = [fileManager attributesOfItemAtPath:filePath error:&error];
           childDir.modificationDate   = [ Utility createDateFormat:[detailDict objectForKey:NSFileModificationDate]];
           childDir.creationDate       = [ Utility createDateFormat:[detailDict objectForKey:NSFileCreationDate]];
           childDir.Size               = [[detailDict objectForKey:NSFileSize]integerValue];
           childDir.dPath              = filePath;

           [dir.childsArray addObject:childDir];
           [childDir release];
        }
        else 
        {
            WEFile *childFile = [self fileAtPath:filePath];
            [dir.childsArray addObject:childFile];

        }
      }
    }

    return dir ;

}
4

1 に答える 1

0

これは私のコードでメモリリークを引き起こすことなく動作します - Leaks/Allocations でチェックしてください:

GAppSettings.hあなたのWEFileクラス

NSImage *gAppIcon;
@property (readwrite, retain)   NSImage  *gAppIcon;

*.m @synthesize gAppIcon;

で - >init

gAppIcon = nil;

で - >dealloc

[ gAppIcon release];

保存と復元にも使用しています->initWithCoder

self.gAppIcon = [ decoder decodeObjectForKey:@"gAppIcon"];

->encodeWithCoder

[ coder encodeObject:   [ self gAppIcon] forKey:    @"gAppIcon"];

- ( id ) copyWithZone:  ( NSZone  *)zone
{
    return self;
}

アイコンは別のクラスで頻繁に読み込まれます。

GAppSettings *appSettings = [ [ [GAppSettings alloc] init] autorelease];

...
NSImage *appIcon = [ [ NSWorkspace sharedWorkspace] iconForFile:completeAppPath];
[ appSettings setGAppIcon:appIcon];
...
return appSettings;

次に、配列に追加されます...

于 2014-04-04T10:32:52.083 に答える