闪屏中的 uiActivityindicatorView 会产生内存泄漏问题吗?
我想在启动屏幕上显示 UIActivityIndicatorView 。 我只是在AppDelegate 中的splashview 上创建一个splashView 和activityindicator。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//[NSThread sleepForTimeInterval:3];
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
splashView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UIImage *splashImage = [UIImage imageNamed:@"Splashimage.png"];
splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashImageView.image = splashImage;
[splashView addSubview:splashImageView];
progressIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(145,440,30,30)];
progressIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[progressIndicator startAnimating];
[splashView addSubview:progressIndicator];
[self.window addSubview:splashView];
[NSThread detachNewThreadSelector:@selector(getInitialData:)
toTarget:self withObject:nil];
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
return YES;
}
- (void)getInitialData:(id)obj {
[NSThread sleepForTimeInterval:3.0];
[splashView removeFromSuperview];
[window addSubview:viewController.view];
}
除了内存泄漏之外,它工作正常。 我在控制台中收到消息,该消息自动释放但没有池 - 只是泄漏。 我做错了什么? 任何帮助将不胜感激。
I want to display UIActivityIndicatorView over splash screen.
I'm just creating a splashView and activityindicator over splashview in AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//[NSThread sleepForTimeInterval:3];
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
splashView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
UIImage *splashImage = [UIImage imageNamed:@"Splashimage.png"];
splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashImageView.image = splashImage;
[splashView addSubview:splashImageView];
progressIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(145,440,30,30)];
progressIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[progressIndicator startAnimating];
[splashView addSubview:progressIndicator];
[self.window addSubview:splashView];
[NSThread detachNewThreadSelector:@selector(getInitialData:)
toTarget:self withObject:nil];
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
return YES;
}
- (void)getInitialData:(id)obj {
[NSThread sleepForTimeInterval:3.0];
[splashView removeFromSuperview];
[window addSubview:viewController.view];
}
It is working fine except memory leakage.
I'm getting message in console that autoreleased with no pool in place - just leaking.
What i m doing wrong?
Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的线程方法 getInitialData 必须创建并耗尽自动释放池。这是针对主线程自动完成的,但不是针对您创建的任何额外线程。只需将其添加到方法的顶部:
并将其添加到底部:
您收到错误消息的原因是因为 viewController.view 返回一个自动释放的对象,并且您在线程上没有适当的自动释放池。
Your thread method getInitialData must create and drain an autorelease pool. This is done automatically for the main thread but not for any extra threads that you create. Just add this at the top of the method:
and this at the bottom:
The reason that you are getting the error message is because viewController.view is returning an autoreleased object and you don't have an autorelease pool in place on the thread.
这里有几个问题。您需要释放您分配的任何内容。变量
splashview
、splashImageView
和progressIndicator
已分配但未释放,因此它们会泄漏。您收到有关 NSAutoreleasePool 的消息是因为您正在单独的线程上执行
getInitialData:
。NSAutoreleasePool
是每个线程的,所以你需要这样做:There's a couple problems here. You need to release anything you alloc. The variables
splashview
,splashImageView
, andprogressIndicator
are allocated but not released, so those will leak.The message you are getting about the NSAutoreleasePool is because you are doing
getInitialData:
on a separate thread.NSAutoreleasePool
s are per-thread, so you you need to do this:您真的需要制作为 ivar/properties 吗?
以下行不关我的事:
Do you really need to make as ivar/properties ?
The following line is not none of my business:
你似乎没有在这里发布任何东西。
You don't seem to be releasing anything here.