我从XCode那里得到了这个错误:
objc[8422]: FREED(id): message release sent to freed object=0x3b120c0
我用谷歌搜索,发现这与记忆有关。但是我不知道哪个代码出错了,有什么想法吗?在模拟器中启动我的应用程序后,它会提示第二个,除了上面的错误之外没有其他错误。
@implementation MyAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[self changeScene:[MainGameMenuScene class]];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
- (void) changeScene: (Class) scene
{
BOOL animateTransition = true;
if(animateTransition){
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES]; //does nothing without this line.
}
if( viewController.view != nil ) {
[viewController.view removeFromSuperview]; //remove view from window's subviews.
[viewController.view release]; //release gamestate
}
viewController.view = [[scene alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) andManager:self];
//now set our view as visible
[window addSubview:viewController.view];
[window makeKeyAndVisible];
if(animateTransition){
[UIView commitAnimations];
}
}
答案 0 :(得分:3)
[viewController.view release]; //release gamestate
这是一个额外的版本(如果你没有分配它,不要发布它)。几条线:
viewController.view = [[scene alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) andManager:self];
将在保留新值之前再次释放viewController.view,这将过度释放视图并导致崩溃(它将释放消息发送到已经被释放的对象/已经释放的内存) 。相反,尝试:
scene *view = [[scene alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) andManager:self];
viewController.view = view; // setView will release the old/retain the new
[view release]; // so you don't need to worry about releasing it later
答案 1 :(得分:0)
得到解决方案:
- (void) changeScene: (Class) scene
{
BOOL animateTransition = true;
if(animateTransition){
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES]; //does nothing without this line.
}
if( viewController.view != nil ) {
[viewController.view retain]; //retain the view.
[viewController.view removeFromSuperview]; //remove view from window's subviews.
[viewController.view release]; //release gamestate
}
viewController.view = [[scene alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) andManager:self];
//now set our view as visible
[window addSubview:viewController.view];
[window makeKeyAndVisible];
if(animateTransition){
[UIView commitAnimations];
}
}
我需要保留视图,但我不知道为什么这是必要的。
答案 2 :(得分:0)
这一行:
[viewController.view release];
很可能是你的问题。除了类dealloc方法之外,永远不要将释放发送到对象的属性。视图控制器视图属性访问器将为您处理保留和释放。