2011-10-07 18 views
0

IKImageBrowserViewで画像を読み込むための目的のCコードでこの問題が発生しています。 私はリンゴの画像ブラウザの例に従っていますが、私はまだいくつかの点で失敗し、メモリ管理を推測しています。 ここでは、コードの一部です:今目的Cメモリ管理の問題

/* Our datasource object */ 
@interface myImageObject : NSObject 
{ 
    NSString *_path; 
} 
@end 
@implementation myImageObject 

- (void)dealloc 
{ 
    [_path release]; 
    [super dealloc]; 
} 

/* our datasource object is just a filepath representation */ 
- (void)setPath:(NSString *)path 
{ 
    NSLog(@"setting path for %@",path); 
    if(_path != path) 
    { 
    [_path release]; 
     _path = [path retain]; 
    } 
} 

、それは私がきちんとオブジェクト内のパスの値を保持していそうです。今上のコントローラコードに :

-(IBAction)loadMosaicImages:(id)sender 
{ 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
NSArray *urls = [openFiles() retain]; 
NSInteger n; 
n = [urls count]; 

NSURL *url = [urls objectAtIndex:0]; 
[self parsePathForImages:[url path]]; 
[urls release]; 
[pool drain]; 
} 
- (void)updateDatasource 
{ 
    NSLog(@" UDS-> _importedImages length : %@",[_importedImages count]); 
    //-- update our datasource, add recently imported items  
    [_images addObjectsFromArray:_importedImages]; 

    //-- empty our temporary array 
    [_importedImages removeAllObjects]; 
    NSLog(@" UDS-> _images length : %@",[_images count]); 
    //-- reload the image browser and set needs display 
    [_imageBrowser reloadData]; 
} 
-(void)parsePathForImages:(NSString *)path{ 

    NSLog(@"Directory within thread method is %@",path); 
    NSArray *content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; 
    myImageObject *p; 
    for(int i=0;i<content.count;i++) 
    { 
     // NSLog(@"%@",(NSString *)[content objectAtIndex:i]); 
     NSLog(@"Complete : %@",[path stringByAppendingPathComponent:(NSString *)[content objectAtIndex:i]]); 
     /* add a path to our temporary array */ 
     p = [[myImageObject alloc] init]; 
     [p setPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]]]; 
     [_importedImages addObject:p]; 
     [p release]; 
    } 
    [self updateDatasource]; 
} 

、それが関連するすべてのコードです。 _imagesと_importedImagesは、awakeからnibメソッドで割り当てられたNSMutableArraysであり、openFiles()はNSOpenpanelを開き、パスのNSArrayを返す静的メソッドです。

Directory within thread method is /Users/cromestant/Code/images/ 
Complete : /Users/cromestant/Code/images/1.jpg 
setting path for /Users/cromestant/Code/images/1.jpg 
Complete : /Users/cromestant/Code/images/2.jpg 
setting path for /Users/cromestant/Code/images/2.jpg 
Complete : /Users/cromestant/Code/images/3.jpg 
setting path for /Users/cromestant/Code/images/3.jpg 

このためデバッグ出力は、これを行います。 。 。 は、NSLogの 'EXEC_BAD_ACCESS'メソッドのupdateDataSourceメソッドの最初の行でクラッシュを停止します。どこでメモリ管理に問題がありますか? 私はautoreleasePoolを作成しているようですので、私は別の場所に保持する時間があります、私はオブジェクトをリリースします..私は本当に問題がどこにあるのか分からない。ありがとうございます。

+0

ところで、実際には関係ないので、投稿に必要なプロトコルメソッドを省略しました。 – cromestant

答えて

2

実際、あなたの問題はメモリ管理ではないかもしれないと思います。それはここにあるかもしれない:count方法が整数ではなく、オブジェクトを返すため

NSLog(@" UDS-> _importedImages length : %@",[_importedImages count]); 

それは

NSLog(@" UDS-> _importedImages length : %d",[_importedImages count]); 

でなければなりません。

+0

あなたは絶対に正しいです、私はこれを馬鹿に感じます。それは私がNSLogsを貼り付けるためにコピーしたものです。%luのために変更されていて、すべてOKです。 – cromestant

+0

基本的にメモリ管理は大丈夫と思われますが、後でアナライザを見て回ります。 – cromestant

+0

心配する必要はありません。 :) –