gpt4 book ai didi

objective-c - 为什么我有内存问题?

转载 作者:行者123 更新时间:2023-12-04 06:56:01 25 4
gpt4 key购买 nike

我从 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];
}
}

最佳答案

[viewController.view release]; //release gamestate

这是一个额外的释放(如果你没有分配它,不要释放它)。几行下来:
viewController.view = [[scene alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) andManager:self]; 

将在保留新值之前再次释放 viewController.view ,这将过度释放 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

关于objective-c - 为什么我有内存问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2567382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com