2011-09-13 155 views
1

我在我的一個視圖控制器的loadView:方法中遇到了一些代碼問題。基本上我有一個視圖,它以一個更大的視圖爲中心(在iPad上),並且有一些標籤,按鈕和圖標,這些標籤,按鈕和圖標以編程方式加載到它中。- [CALayer發佈]:發送到已釋放實例的消息

當視圖控制器調用dealloc方法並嘗試釋放時,會發生此問題。我得到一個- [CALayer發佈]:消息發送到解除分配的實例錯誤和應用程序崩潰。

從閱讀有關此錯誤,它似乎我過度釋放的東西,但我不認爲是這種情況:下面的代碼是麻煩製造商,我已經嘗試過所有的自動釋放和手動釋放調用,以及根本不釋放他們(我知道,我知道,這是不好的),但它繼續導致havok。

container = [[UIView alloc] initWithFrame:CGRectNull]; 
container.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin); 
container.frame = CGRectMake((viewSize.width-320)/2, (viewSize.height-480)/2, 320, 480); 
[self.view addSubview:container]; 

UILabel *welcomeTo = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease]; 
welcomeTo.backgroundColor = [UIColor clearColor]; 
welcomeTo.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18.0]; 
welcomeTo.frame = CGRectMake(23, 15, 103, 24); 
welcomeTo.shadowColor = [UIColor blackColor]; 
welcomeTo.shadowOffset = CGSizeMake(0, 1); 
welcomeTo.text = @"Welcome to"; 
welcomeTo.textAlignment = UITextAlignmentLeft; 
welcomeTo.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:welcomeTo]; 

UILabel *progressions = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease]; 
progressions.backgroundColor = [UIColor clearColor]; 
progressions.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:44.0]; 
progressions.frame = CGRectMake(20, 30, 280, 56); 
progressions.shadowColor = [UIColor blackColor]; 
progressions.shadowOffset = CGSizeMake(0, 1); 
progressions.text = @"Progressions"; 
progressions.textAlignment = UITextAlignmentCenter; 
progressions.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:progressions]; 

UILabel *blurb = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease]; 
blurb.backgroundColor = [UIColor clearColor]; 
blurb.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0]; 
blurb.frame = CGRectMake(23, 100, 274, 51); 
blurb.numberOfLines = 3; 
blurb.shadowColor = [UIColor blackColor]; 
blurb.shadowOffset = CGSizeMake(0, 1); 
blurb.text = @"Kick off the fun by adding some content, or if you're not sure what you're doing, check out the \"Getting Started\" guide!"; 
blurb.textAlignment = UITextAlignmentLeft; 
blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:blurb]; 

UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(20, 170, 280, 88)]; 
[button1 setBackgroundImage:[UIImage imageNamed:@"emptyButtonLarge.png"] forState:UIControlStateNormal]; 
[button1 setBackgroundImage:[UIImage imageNamed:@"emptyButtonLargeHighlighted.png"] forState:UIControlStateHighlighted]; 
[button1 addTarget:self action:@selector(createChart:) forControlEvents:UIControlEventTouchUpInside]; 
[container addSubview:button1]; 
[button1 release]; 

UIImageView *icon1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addchart.png"]]; 
icon1.frame = CGRectMake(30, 180, icon1.frame.size.width, icon1.frame.size.height); 
[container addSubview:icon1]; 
[icon1 release]; 

UILabel *button1title = [[[UILabel alloc] initWithFrame:CGRectMake(110, 185, 180, 20)] autorelease]; 
button1title.backgroundColor = [UIColor clearColor]; 
button1title.font = [UIFont boldSystemFontOfSize:17.0]; 
button1title.shadowColor = [UIColor blackColor]; 
button1title.shadowOffset = CGSizeMake(0, 1); 
button1title.text = @"Create a Chart"; 
button1title.textAlignment = UITextAlignmentLeft; 
button1title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:button1title]; 

UILabel *button1blurb = [[[UILabel alloc] initWithFrame:CGRectMake(110, 205, 180, 40)] autorelease]; 
button1blurb.backgroundColor = [UIColor clearColor]; 
button1blurb.font = [UIFont systemFontOfSize:13.0]; 
button1blurb.numberOfLines = 2; 
button1blurb.shadowColor = [UIColor blackColor]; 
button1blurb.shadowOffset = CGSizeMake(0, 1); 
button1blurb.text = @"A cheat sheet for a displaying a song in any key."; 
button1blurb.textAlignment = UITextAlignmentLeft; 
button1blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:button1blurb]; 

UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(20, 270, 280, 88)]; 
[button2 setBackgroundImage:[UIImage imageNamed:@"emptyButtonLarge.png"] forState:UIControlStateNormal]; 
[button2 setBackgroundImage:[UIImage imageNamed:@"emptyButtonLargeHighlighted.png"] forState:UIControlStateHighlighted]; 
[button2 addTarget:self action:@selector(createSet:) forControlEvents:UIControlEventTouchUpInside]; 
[container addSubview:button2]; 
[button2 release]; 

UIImageView *icon2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addset.png"]]; 
icon2.frame = CGRectMake(30, 280, icon2.frame.size.width, icon2.frame.size.height); 
[container addSubview:icon2]; 
[icon2 release]; 

UILabel *button2title = [[[UILabel alloc] initWithFrame:CGRectMake(110, 285, 180, 20)] autorelease]; 
button2title.backgroundColor = [UIColor clearColor]; 
button2title.font = [UIFont boldSystemFontOfSize:17.0]; 
button2title.shadowColor = [UIColor blackColor]; 
button2title.shadowOffset = CGSizeMake(0, 1); 
button2title.text = @"Create a Set"; 
button2title.textAlignment = UITextAlignmentLeft; 
button2title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:button2title]; 

UILabel *button2blurb = [[[UILabel alloc] initWithFrame:CGRectMake(110, 305, 180, 40)] autorelease]; 
button2blurb.backgroundColor = [UIColor clearColor]; 
button2blurb.font = [UIFont systemFontOfSize:13.0]; 
button2blurb.numberOfLines = 2; 
button2blurb.shadowColor = [UIColor blackColor]; 
button2blurb.shadowOffset = CGSizeMake(0, 1); 
button2blurb.text = @"A collection of charts that can be displayed like a book."; 
button2blurb.textAlignment = UITextAlignmentLeft; 
button2blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:button2blurb]; 

UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(20, 370, 280, 40)]; 
[button3 setBackgroundImage:[UIImage imageNamed:@"emptyButtonSmall.png"] forState:UIControlStateNormal]; 
[button3 setBackgroundImage:[UIImage imageNamed:@"emptyButtonSmallHighlighted.png"] forState:UIControlStateHighlighted]; 
[button3 addTarget:self action:@selector(gettingStarted:) forControlEvents:UIControlEventTouchUpInside]; 
[container addSubview:button3]; 
[button3 release]; 

UILabel *button3title = [[[UILabel alloc] initWithFrame:CGRectMake(20, 370, 280, 36)] autorelease]; 
button3title.backgroundColor = [UIColor clearColor]; 
button3title.font = [UIFont boldSystemFontOfSize:17.0]; 
button3title.shadowColor = [UIColor blackColor]; 
button3title.shadowOffset = CGSizeMake(0, 1); 
button3title.text = @"Getting Started"; 
button3title.textAlignment = UITextAlignmentCenter; 
button3title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:button3title]; 

UILabel *version = [[[UILabel alloc] initWithFrame:CGRectMake(20, 420, 280, 20)] autorelease]; 
version.backgroundColor = [UIColor clearColor]; 
version.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0]; 
version.shadowColor = [UIColor blackColor]; 
version.shadowOffset = CGSizeMake(0, 1); 
version.text = [NSString stringWithFormat:@"Version v%@ (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], kRevisionNumber]; 
version.textAlignment = UITextAlignmentCenter; 
version.textColor = [UIColor colorWithWhite:1.0 alpha:0.6]; 
[container addSubview:version]; 

容器財產被釋放的dealloc方法,具體如下:

- (void)dealloc { 
    [leather release]; 
    [backButton release]; 
    [container release]; 
    [super dealloc]; 
} 

人提供的任何答案或建議,將不勝感激!我真的很難過。

+0

你試評這一切並嘗試運行? – Nekto

+0

你在代碼上運行分析嗎?從產品菜單中選擇「分析」。修復所有的錯誤。然後,使用工具 - >泄漏來剖析應用程序。注意你的代碼創建的任何紅色泄漏標記(忽略蘋果庫的東西)。 –

+0

@Nekto - 我做到了。當所有上面的代碼被註釋掉時它工作正常。 –

回答

47

用「殭屍」樂器在模擬器中描述您的應用程序。

運行你的應用程序一段時間,做任何你必須做的事情,使你的應用程序崩潰。如果你的小箭頭,點擊旁邊的地址(0x158b3c00)

Zombie accessed

然後..:當它,你會得到一個彈出像下面的圖片,它會阻止應用程序的性能分析它將帶您到達釋放對象(殭屍)的對象保留/釋放歷史記錄。

如果選中上面對保留伯爵走到-1行,並打開查看 - >擴展的細節,它應該指向你在那裏對象被overreleased在你的代碼的堆棧跟蹤和線路:

Stack trace

如果您雙擊它正在發生的類,它會打開你的源,並顯示你的代碼的壞行:

over released code

+0

+1爲您的努力! – Jeremy

+0

+1 bht alla man。 – NaXir

+0

+1解釋和圖片如何追溯到殭屍 – Niro

0

過度釋放可能發生在對象的生命週期中,而不僅僅是當它們被創建時。一些提示:

  • 使用存取,以確保指針設置爲nil
  • 與您使用的release/autorelease一致。我個人更喜歡在一行上有[[[Class alloc] init] autorelease]
  • 瞭解release/autorelease之間的區別。 Let's Build NSAutoreleasePool是一個很好的開始。
相關問題