闪屏中的 uiActivityindicatorView 会产生内存泄漏问题吗?

发布于 2024-12-06 21:33:21 字数 1576 浏览 0 评论 0原文

我想在启动屏幕上显示 UIActivityIndi​​catorView 。 我只是在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 技术交流群。

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

发布评论

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

评论(4

自由如风 2024-12-13 21:33:22

您的线程方法 getInitialData 必须创建并耗尽自动释放池。这是针对主线程自动完成的,但不是针对您创建的任何额外线程。只需将其添加到方法的顶部:

NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];

并将其添加到底部:

[localpool drain];

您收到错误消息的原因是因为 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:

NSAutoreleasePool* localpool = [[NSAutoreleasePool alloc] init];

and this at the bottom:

[localpool drain];

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.

冰魂雪魄 2024-12-13 21:33:22

这里有几个问题。您需要释放您分配的任何内容。变量 splashviewsplashImageViewprogressIndicator 已分配但未释放,因此它们会泄漏。

您收到有关 NSAutoreleasePool 的消息是因为您正在单独的线程上执行 getInitialData:NSAutoreleasePool 是每个线程的,所以你需要这样做:

-(void)getInitialData:(id)obj {
    NSAutoreleasePool pool = [NSAutoreleasePool new];
    [NSThread sleepForTimeInterval:3.0]; 
    [splashView removeFromSuperview];
    [window addSubview:viewController.view];
    [pool release];
} 

There's a couple problems here. You need to release anything you alloc. The variables splashview, splashImageView, and progressIndicator 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. NSAutoreleasePools are per-thread, so you you need to do this:

-(void)getInitialData:(id)obj {
    NSAutoreleasePool pool = [NSAutoreleasePool new];
    [NSThread sleepForTimeInterval:3.0]; 
    [splashView removeFromSuperview];
    [window addSubview:viewController.view];
    [pool release];
} 
蓝海似她心 2024-12-13 21:33:22

您真的需要制作为 ivar/properties 吗?

UIActivityIndicatorView* progressIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(145,440,30,30)];
progressIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[progressIndicator startAnimating];
[splashView addSubview:progressIndicator];
[progressIndicator release];              // release
[self.window addSubview:splashView];
[splashView release];      // release

以下行不关我的事:

我不知道,但我第一次看到有人添加了
初始图像上的活动指示器。以及为什么你需要有
splashImageView,你可以直接在你的plist中添加一个条目
LaunchImage 密钥输入文件

Do you really need to make as ivar/properties ?

UIActivityIndicatorView* progressIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(145,440,30,30)];
progressIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[progressIndicator startAnimating];
[splashView addSubview:progressIndicator];
[progressIndicator release];              // release
[self.window addSubview:splashView];
[splashView release];      // release

The following line is not none of my business:

I do not know, but first time i am seeing that some one is adding the
activity indicator on splash image. and why do you need to have
splashImageView, you could have directly make an entry in your plist
file for LaunchImage key entry

你曾走过我的故事 2024-12-13 21:33:21

你似乎没有在这里发布任何东西。

You don't seem to be releasing anything here.

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