2012-02-25 3 views
0

は、私はライン上NZombieを取得しています理由として失われたビット午前:メモリ管理アドバイス - ゾンビを扱う方法?以下のサンプルコードで

[Category getInitialDataToDisplay:[self getDBPath]]; 

私はSOの記事やその他の文書を通じて見えたが、Objective-Cのと午前に新しいですしていますこれに私の車輪を回転させる。

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

//Copy database to the user's phone if needed. 
[self copyDatabaseIfNeeded]; 

// Init the Array 
activeCategories = [[NSMutableArray alloc] init]; 
activeSubjects = [[NSMutableArray alloc] init]; 
categories = [[NSMutableArray alloc] init]; 
subjects = [[NSMutableArray alloc] init]; 
quotes = [[NSMutableArray alloc] init]; 
quoteMaps = [[NSMutableArray alloc] init]; 

//Initialize the Category array. 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
self.categories = tempArray; 
[tempArray release]; 

//Once the db is copied, get the initial data to display on the screen. 
[Category getInitialDataToDisplay:[self getDBPath]]; 


//populate active subjects and categories: 

activeCategories = [self getActiveCategories]; 
activeSubjects = [self getActiveSubjects]; 

// sort data 

NSSortDescriptor *categorySorter; 
NSSortDescriptor *subjectSorter; 

categorySorter = [[NSSortDescriptor alloc]initWithKey:@"category_title" ascending:YES]; 
subjectSorter = [[NSSortDescriptor alloc]initWithKey:@"title" ascending:YES]; 

NSArray *sortDescriptorsCat = [NSArray arrayWithObject:categorySorter]; 
NSArray *sortDescriptorsSub = [NSArray arrayWithObject:subjectSorter]; 

[self.categories sortUsingDescriptors:sortDescriptorsCat]; 
[self.subjects sortUsingDescriptors:sortDescriptorsSub]; 

[categorySorter release]; 
[subjectSorter release]; 

// Configure and show the window 
[window addSubview:[navigationController view]]; 
[window makeKeyAndVisible]; 
} 

...ここ

- (void)dealloc { 

[activeSubjects release]; 
[activeCategories release]; 

[categories autorelease]; 
[subjects autorelease]; 
[quotes autorelease]; 
[quoteMaps autorelease]; 
[navigationController release]; 
[window release]; 
[super dealloc]; 
} 

はgetInitialDataToDisplayです:ここでは

+ (void) getInitialDataToDisplay:(NSString *)dbPath { 

// Use this section to bring in database and populate the array 
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];  
[database open]; 

QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate]; 


//appDelegate.categories = [appDelegate.categories sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; 

//POPULATE THE SUBJECT 
FMResultSet *result_subjects = [database executeQuery:@"select * from SUBJECT"]; 

while([result_subjects next]) { 

    NSInteger primaryKey = [result_subjects intForColumn:@"SUBJECT_ID"]; 
    Subject *sub = [[Subject alloc] initWithPrimaryKey:primaryKey]; 

    sub.title = [result_subjects stringForColumn:@"SUBJECT"]; 
    sub.category_title = [result_subjects stringForColumn:@"CATEGORY"]; 
    sub.active = [result_subjects intForColumn:@"ACTIVE"]; 
    sub.isDirty = NO; 

    [appDelegate.subjects addObject:sub]; 
    [sub release]; 

} 

FMResultSet *result_categories = [database executeQuery:@"select distinct category from SUBJECT"]; 

while([result_categories next]) { 

    Category *cat = [[Category alloc] init]; 

    cat.category_title = [result_categories stringForColumn:@"CATEGORY"]; 
    NSLog(@"loading category: %@", cat.category_title); 

    QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    for (Subject *sb in appDelegate.subjects){ 

     if([cat.category_title isEqualToString:sb.category_title]){ 
      [cat.subjects addObject:sb]; 
      NSLog(@" Adding subject: %@ cat.subjects.count=%i", sb.title, cat.subjects.count); 

     } 

    } 

    [appDelegate.categories addObject:cat]; 
    [cat release]; 

} 

//POPULATE THE QUOTES 
FMResultSet *result_quotes = [database executeQuery:@"select * from QUOTE"]; 

while([result_quotes next]) { 

    Quote *sub = [Quote alloc]; 

    sub.quote_id = [result_quotes stringForColumn:@"QUOTE_ID"]; 
    sub.quote_date = [result_quotes stringForColumn:@"DATE"]; 
    sub.title = [result_quotes stringForColumn:@"DESC1"]; 
    sub.desc2 = [result_quotes stringForColumn:@"DESC2"]; 
    sub.excerpt = [result_quotes stringForColumn:@"EXCERPT"]; 
    sub.note = [result_quotes stringForColumn:@"NOTES"]; 
    sub.isDirty = NO; 

    [appDelegate.quotes addObject:sub]; 
    [sub release]; 

}  


//POPULATE THE QUOTE_MAPS 
FMResultSet *result_quote_map = [database executeQuery:@"select * from QUOTE_MAP"]; 

while([result_quote_map next]) { 

    QuoteMap *sub = [QuoteMap alloc]; 

    sub.quote_id = [result_quote_map stringForColumn:@"QUOTE_ID"]; 
    sub.quote_map_id = [result_quote_map stringForColumn:@"QUOTE_MAP_ID"]; 
    sub.subject_id = [result_quote_map stringForColumn:@"SUBJECT_ID"]; 
    sub.isDirty = NO; 

    [appDelegate.quoteMaps addObject:sub]; 
    [sub release]; 

}  

[database close]; 

NSLog(@"Count of categories: %i", appDelegate.categories.count); 
NSLog(@"Count of subjects: %i", appDelegate.subjects.count); 
NSLog(@"Count of quotes: %i", appDelegate.quotes.count); 
NSLog(@"Count of quoteMaps: %i", appDelegate.quoteMaps.count); 

} 

はgetDbPathです:

- (NSString *) getDBPath { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
return [documentsDir stringByAppendingPathComponent:@"reference.db"]; 
} 
+0

....それが役立つと思いますか?また、カテゴリgetInitialDataToDisplay:classメソッドを表示することもできます。 – Jamie

+3

とあなたの[self getDBPath]メソッド。 – Jamie

+1

また、deallocで[object ** autorelease **]を使用しないでください。 [オブジェクトの解放]を使用します。 –

答えて

2

時々、行うための最善のことは、ビルド - >(CMDシフトB)を分析している。これは、ほとんどすべての場合にはすぐにバグを指摘します。運の

ベスト!

+0

ありがとうございましたNico、私はBuild + Analyzeの外観が気に入っていますが、正直なところ私はデータをどうするか分かりません。それは私の問題です。私はオブジェクトを適切に管理し、方向性を必要とする人物を知ろうとしています。 – jroyce

+0

青いタブが生成されます。タブをクリックすると、問題の原因と思われる行がポイントされます。 https://developer.apple.com/library/mac/#documentation/IDEs/Conceptual/Xcode4TransitionGuide/Debugging/Debugging.html – Nico

1
categories = [[NSMutableArray alloc] init]; 
. 
. 
//Initialize the Category array. 
NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
self.categories = tempArray; 
[tempArray release]; 

セットアップカテゴリを設定してから、tempArrayを設定し、それをカテゴリに置き換えてリークを作成し、次にtempObjectをリリースしました。どのカテゴリも指しているので、"self.categories"は保持されたプロパティゾンビ。そこに何か問題があるようです。 私はプロパティ宣言と確実にする、それらの合成(あなたのコードのいくつかの詳細を参照する必要があるかもしれません。

ゾンビが「getInitialDataToDisplay」または「getDBPath」で呼び出され ピンポイントを知るために2行にそれを分割してみてくださいより

0

私は思います.hファイルでCategoryをretainプロパティとして宣言していません。そうでなければ、次の行を追加してくださいあなたの.hファイル

@property (nonatomic, retain) NSArray Category; 

そして

@synthesize Category; 

AS-.Mでプロパティを合成して、私はあなたがARCを使用していないか、しています