iPhone 应用程序崩溃,在 dealloc 中释放后第二次出现访问错误

发布于 2024-12-25 01:36:29 字数 1207 浏览 1 评论 0原文

我正面临崩溃“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杯别 2025-01-01 01:36:29

试试这个代码

VideoViewController *videoScreen = [VideoViewController new];

    UINavigationController *navContr = [[UINavigationController alloc] initWithRootViewController:videoScreen];

    navContr.navigationBar.tintColor = kNavbarColor;

    [self presentModalViewController:navContr animated:NO];

    [navContr release];

 [videoScreen release];

Try this code

VideoViewController *videoScreen = [VideoViewController new];

    UINavigationController *navContr = [[UINavigationController alloc] initWithRootViewController:videoScreen];

    navContr.navigationBar.tintColor = kNavbarColor;

    [self presentModalViewController:navContr animated:NO];

    [navContr release];

 [videoScreen release];
深海里的那抹蓝 2025-01-01 01:36:29

如果你已经将player声明为带有retain的属性,那么你需要这样写:

MPMoviePlayerController* tmp = [[MPMoviePlayerController alloc] 
                                 initWithContentURL:movieURL];
self.player = tmp;
[tmp release];
self.player.movieSourceType = MPMovieSourceTypeStreaming;

..

并在dealloc中:

self.player = nil;

if you have declared player as a property with retain then you need to write like this instead:

MPMoviePlayerController* tmp = [[MPMoviePlayerController alloc] 
                                 initWithContentURL:movieURL];
self.player = tmp;
[tmp release];
self.player.movieSourceType = MPMovieSourceTypeStreaming;

..

and in dealloc:

self.player = nil;
各空 2025-01-01 01:36:29

您的应用程序崩溃的原因是因为您试图释放当前正在运行的 new 创建的基础对象

  • new 不支持自定义初始值设定项(如 initWithString)

  • alloc-init 比 new 更明确

  • new 在 autorelease 中没有比 alloc-init 正确处理,因此你从内存中释放它杀死的对象会导致应用程序崩溃

    VideoViewController *videoScreen = [[VideoViewController alloc] init];
    
    UINavigationController *navContr = [[UINavigationController alloc] initWithRootViewController:videoScreen];
    
    navContr.navigationBar.tintColor = kNavbarColor;
    
    [自我呈现ModalViewController:navContr动画:否];
    
    [视频画面发布];
    
    [navContr发布];
    

    你的代码的其余部分都很好,因为我在我们的应用程序中使用了这样的代码,它工作得很好,一切都很好。

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

    VideoViewController *videoScreen = [[VideoViewController alloc] init];
    
    UINavigationController *navContr = [[UINavigationController alloc] initWithRootViewController:videoScreen];
    
    navContr.navigationBar.tintColor = kNavbarColor;
    
    [self presentModalViewController:navContr animated:NO];
    
    [videoScreen release];
    
    [navContr release];
    

    rest of your code are fine because I used like this code for our application it works fine, all the best.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文