2011-09-28 55 views
0

如何解決內存泄漏在此:保留內存泄漏在iphone菜單項

NSArray *keyboard = [[[NSDictionary alloc] initWithContentsOfFile:menuPath] objectForKey:@"Menu"]; 
[self setMenuItems:keyboard]; 
[keyboard release]; 

setMenuItems在頭文件中定義。

@property (nonatomic,retain) NSArray *menuItems; 

回答

-1

在dealloc方法

- (void)dealloc 
{ 
self.menuItems = nil; 
[super dealloc]; 
} 

NSArray *keyboard = [[[NSDictionary alloc] initWithContentsOfFile:menuPath] objectForKey:@"Menu"]; 
self.menuItems = keyboard; 
[keyboard release]; 
+0

儀器仍然顯示內存泄漏!任何想法爲什麼! –

+1

這是完全錯誤的。 – Rog

1
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:menuPath]; 
self.menuItems = [dict objectForKey:@"Menu"]; 
[dict release]; 

而在你的dealloc方法

- (void)dealloc 
{ 
    [menuItems release], menuItems = nil; 
}