2012-02-25 24 views
0

在下面的示例代碼,我有點失落,爲什麼我就行得到一個NZombie:內存管理建議 - 如何處理殭屍?

[Category getInitialDataToDisplay:[self getDBPath]]; 

我已經經歷了這麼員額和其他文件看起來卻是新來的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

您是否使用ARC或不?此外,也許你可以顯示你的類別getInitialDataToDisplay:類的方法。 – Jamie 2012-02-25 02:52:41

+3

和你的[self getDBPath]方法。 – Jamie 2012-02-25 02:59:07

+1

另外你不應該在dealloc中使用[object ** autorelease **]。使用[object release]。 – 2012-02-25 03:09:55

回答

2

有時候,做的最好的事情就是建設 - >分析(CMD轉向b),這將在幾乎所有情況下指出你錯誤的時候了。

好運!

+0

感謝Nico,我喜歡Build + Analyze的外觀,但老實說我不知道​​該如何處理數據。這就是我的問題 - 我試圖瞭解誰能更好地管理對象並需要一些指導。 – jroyce 2012-02-25 05:55:48

+0

它會生成一個藍色標籤,如果你點擊標籤,它會指向它認爲會導致問題的行。 https://developer.apple.com/library/mac/#documentation/IDEs/Conceptual/Xcode4TransitionGuide/Debugging/Debugging.html – Nico 2012-02-26 05:10:28

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

我設置了類別,然後設置tempArray,用它替換類別中的一個,從而進行泄漏,然後釋放臨時arrayObject,哪些類別現在也指向,因此除非"self.categories"是保留屬性,否則它將是殭屍。那裏似乎有什麼問題。 我可能需要看到更多的代碼(財產申報和它們的合成,以確保。

是殭屍呼籲「getInitialDataToDisplay」或「getDBPath」 請嘗試將它放在2號線就知道針點更

0

我想您還沒有將.h文件中的類別聲明爲保留屬性。如果不是,請添加以下行在您的.h文件中

@property (nonatomic, retain) NSArray Category; 

和綜合原樣

@synthesize Category; 

我認爲這將有助於在.M財產....