ios - 对象在初始化后立即释放

标签 ios objective-c uiwebview youtube

我正在尝试制作一个播放 YouTube 视频的类(class),但我遇到了几个问题。

这是我处理 YouTube 视频的类:

// this is the only property declared in the .h file:
@property (strong, nonatomic) UIView * view

// the rest of this is the .m file:
#import "MyYouTube.h"
@interface MyYouTube()
@property (strong, nonatomic) NSDictionary * contentData;
@property (strong, nonatomic) UIWebView * webView;
@property (nonatomic) int videoOffset;
@end

@implementation MyYouTube
@synthesize view,contentData,webView,videoOffset;

- (MyYouTube*) initIntoView: (UIView*) passedView withContent: (NSDictionary*) contentDict {
    NSLog(@"YOUTUBE: begin init");
    self=[super init];
    videoOffset=0;
    view=[[UIView alloc] initWithFrame:passedView.bounds];
    [view setBackgroundColor:[UIColor blackColor]];
    [view setAutoresizesSubviews:YES];
    [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
    contentData=contentDict;

    NSString * compiledUrl=[[NSString alloc] initWithFormat:@"http://_xxx_.com/app/youtube.php?yt=%@",[contentData objectForKey:@"cnloc"]];
    NSURL * url=[[NSURL alloc] initWithString:compiledUrl];
    NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url];

    webView=[[UIWebView alloc] initWithFrame:passedView.bounds];
    [webView loadRequest:request];
    [webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    [[webView scrollView] setScrollEnabled:NO];
    [[webView scrollView] setBounces:NO];
    [webView setMediaPlaybackRequiresUserAction:NO];
    [webView setDelegate:self];

    NSLog(@"YOUTUBE: self: %@",self);
    NSLog(@"YOUTUBE: delegate: %@",webView.delegate);

    [view addSubview:webView];
    NSLog(@"YOUTUBE: end init");
    return self;
}

-(void)webViewDidFinishLoad:(UIWebView*)myWebView {
    NSLog(@"YOUTUBE: send play command");
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"playVideo(%d)", videoOffset]];
}

- (void) dealloc {
    NSLog(@"YOUTUBE: dealloc");
}
@end

下面是调用这个类的代码(这段代码位于appDelegate中):

NSLog(@"about to load youtube");
ytObj=[[MyYouTube alloc] initIntoView:mainView withContent:cn];
NSLog(@"loaded youtube");
[mainView addSubview:[ytObj view]];

仅供引用,mainView 和 ytObj 声明如下:

@property (nonatomic,strong) UIView * mainView;
@property (nonatomic,strong) MyYouTube * ytObj;

运行此代码时,应用程序崩溃,我在控制台中看到:

about to load youtube
YOUTUBE: begin init
YOUTUBE: self: <MyYouTube: 0x16503d40>
YOUTUBE: delegate: <MyYouTube: 0x16503d40>
YOUTUBE: end init
YOUTUBE: dealloc
loaded youtube
*** -[MyYouTube respondsToSelector:]: message sent to deallocated instance 0x166f19f0

如果我将 UIWebView 委托(delegate)设置为 nil,则应用不会崩溃,但正如预期的那样,YouTube 视频不会自动播放。

谁能给我解释一下:

  • 为什么对象在分配后立即释放 初始化了吗?

  • 为什么 respondsToSelector: 消息被发送到 除了 MyYouTube 以外的实例?

  • 如何让 YouTube 视频在应用程序不崩溃的情况下自动播放?

非常感谢。


编辑

完全是我的错 - ytObj 是一个强大的属性 - 我忘了提及这一点。我添加了代码来反射(reflect)这一点。


编辑 2

我在 dealloc 方法上添加了一个断点,这是调用堆栈:

0 -[MyYouTube dealloc]
1 objc-object::sidetable_release(bool)
2 -[MyAppDelegate playYouTube:]

这里的最后一个条目 (playYouTube:) 是包含上面原始帖子中我的应用程序委托(delegate)中的代码的方法。那么,还有一个问题:

  • 什么是 objc-object::sidetable_release(bool),它有什么作用,为什么要释放我的 YouTube 对象?

最佳答案

Why does the object deallocates immediately after it has been initialised?

因为没有人拥有它。

您将其设置为 View 的委托(delegate),但UIWebView 的委托(delegate)属性是一个assign 属性。该 View 不拥有其委托(delegate)的所有权。这意味着当 ytObj 超出范围时(可能之前,取决于优化)没有任何东西拥有它,所以它消失了。

编辑

您还需要确保当 ytObj 被释放时,您将它仍然是委托(delegate)的任何 View 的委托(delegate)属性设置为 nil。否则, View 将继续尝试向已释放的对象发送消息。

How can I get the YouTube video to autoplay without the app crashing?

您需要确保 ytObj 的持续时间与它作为委托(delegate)的 View 一样长,并且当它被释放时,它的 View 委托(delegate)设置为 nil。


另一个小问题。你的 -init 函数应该在调用 self = [super init] 后测试 self 不是 nil。如果 self 为 nil,它不应该运行任何其余的初始化代码。

关于ios - 对象在初始化后立即释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20239930/

相关文章:

ios - Firebase 实时数据库,检查 firebase 数据库值是否存在?

android - App Store 审核团队报告使用 Firebase 存储时出现 'Connection refused' 错误,阻止图像加载

iphone - 为什么addSubview会异步加载 View

ios - 如何在 html 文件上链接本地 pdf 文件 - html 文件显示在 UIWebView

iphone - 保存和恢复 UIWebView 浏览 session

ios - 使用自动布局的 uitableview 页脚 View 的运行时扩展

javascript - 从 HTML 中提取 JavaScript 代码中的变量值

iphone - iOS 5 中的方法调配?

objective-c - 添加 UITapGestureRecognizer 后 UIButtons 将不起作用

ios - 为什么导航栏在 UIViewWeb 之上? (OBJ-C 初学者)