iPhone 应用程序崩溃,在 dealloc 中释放后第二次出现访问错误
我正面临崩溃“Exc_Bad_Access”我正在编写以下代码。你们中的任何人如果回答都会非常有帮助,谢谢。我正在尝试以横向视图播放视频,而其余应用程序保持纵向。以下工作完全正常,但是当我第二次打开它时,第二次调用 openMoviePlayer 时它崩溃了。我知道 Exc_Bad_Access 有时是不可预测的或查明原因,但相信我,这是非常小的代码,每次都会在这里崩溃,而我确信其他代码都很好。 是的,如果我从 dealloc 中删除 [玩家释放],它工作正常,但我知道当时保留的玩家数量为 1,这是内存泄漏。
-(void) openMoviePlayer{
VideoViewController *videoScreen = [VideoViewController new];
UINavigationController *navContr = [[UINavigationController alloc] initWithRootViewController:videoScreen];
navContr.navigationBar.tintColor = kNavbarColor;
[self presentModalViewController:navContr animated:NO];
[videoScreen release];
[navContr release];
}
VideoVIewController.m
- (void)viewDidLoad {
[super viewDidLoad];
player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
player.movieSourceType = MPMovieSourceTypeStreaming;
player.controlStyle = MPMovieControlStyleDefault;
[player.view setFrame:CGRectMake(0.0, 0.0, 480.0, 270.0)];
[self.view addSubview:player.view];
[player play];
}
- (void)dealloc {
[player release];
[super dealloc];
}
谢谢为了你的帮助!
I am facing a crash "Exc_Bad_Access" I am writing following code. Any of you if answer it could be very helpful thanks. I am trying to play a video in landscape view while rest app remains in portrait orientation. Following works perfectly fine but when I open it second time that is calling openMoviePlayer second time it crashes. I know Exc_Bad_Access sometime is unpredictable or pin point the reason, But trust me it's very small code and it crashes right here everytime while other code is fine I am sure.
AND YES IF I REMOVE [player release] from dealloc it works fine but I know at that time retain count of player is 1 and that's a memory leak.
-(void) openMoviePlayer{
VideoViewController *videoScreen = [VideoViewController new];
UINavigationController *navContr = [[UINavigationController alloc] initWithRootViewController:videoScreen];
navContr.navigationBar.tintColor = kNavbarColor;
[self presentModalViewController:navContr animated:NO];
[videoScreen release];
[navContr release];
}
VideoVIewController.m
- (void)viewDidLoad {
[super viewDidLoad];
player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
player.movieSourceType = MPMovieSourceTypeStreaming;
player.controlStyle = MPMovieControlStyleDefault;
[player.view setFrame:CGRectMake(0.0, 0.0, 480.0, 270.0)];
[self.view addSubview:player.view];
[player play];
}
- (void)dealloc {
[player release];
[super dealloc];
}
Thanks for your help !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个代码
Try this code
如果你已经将player声明为带有retain的属性,那么你需要这样写:
并在dealloc中:
if you have declared player as a property with retain then you need to write like this instead:
and in dealloc:
您的应用程序崩溃的原因是因为您试图释放当前正在运行的 new 创建的基础对象
new 不支持自定义初始值设定项(如 initWithString)
alloc-init 比 new 更明确
new 在 autorelease 中没有比 alloc-init 正确处理,因此你从内存中释放它杀死的对象会导致应用程序崩溃
你的代码的其余部分都很好,因为我在我们的应用程序中使用了这样的代码,它工作得很好,一切都很好。
Your application crashed why because you are trying to release the base object created with new is running currently
new doesn't support custom initializers (like initWithString)
alloc-init is more explicit than new
new is not handled in autorelease properly than alloc - init, so you release the object it killed from the memory results in application crash
rest of your code are fine because I used like this code for our application it works fine, all the best.