为什么 Instruments 在 UIColor 和 NSArray 上标记内存泄漏?

发布于 2024-09-10 01:43:56 字数 1563 浏览 11 评论 0原文

我无法意识到为什么这段代码被标记为存在内存泄漏:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:[NSBundle mainBundle]];
    navigationController = [[UINavigationController alloc] initWithRootViewController:menuView];
    navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]; // memory leak here 47,1%
    [menuView release];
    BilheteViewController *rightView = [[BilheteViewController alloc] initWithNibName:@"BilheteView" bundle: [NSBundle mainBundle]];
    spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil]; // memory leak here 52,9%
    [window addSubview:spliViewController.view];

    [window makeKeyAndVisible];

    [rightView release];

    return YES;
}

只是标有问题的行:

navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]

以及

spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil];

如何解决此泄漏?

更新1

App Delegate的dealloc方法,navigationController和spliViewController都被释放:

- (void)dealloc {
    [navigationController release];
    [spliViewController release];
    [window release];
    [super dealloc];
}

I couldn't realize why this code is being marked as having memory leaks:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:[NSBundle mainBundle]];
    navigationController = [[UINavigationController alloc] initWithRootViewController:menuView];
    navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]; // memory leak here 47,1%
    [menuView release];
    BilheteViewController *rightView = [[BilheteViewController alloc] initWithNibName:@"BilheteView" bundle: [NSBundle mainBundle]];
    spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil]; // memory leak here 52,9%
    [window addSubview:spliViewController.view];

    [window makeKeyAndVisible];

    [rightView release];

    return YES;
}

Just the lines marked with problems:

navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]

and

spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil];

How do I solve this leaks?

Update 1

App Delegate's dealloc method, both navigationController and spliViewController are being released:

- (void)dealloc {
    [navigationController release];
    [spliViewController release];
    [window release];
    [super dealloc];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

下雨或天晴 2024-09-17 01:43:56

泄漏工具会向您显示泄漏的内容的分配位置。

所以它的意思是,在您分配这些值之后,它们从未被释放 - 这似乎意味着您的导航控制器没有在应该被释放的时候被释放。

The leaks tool shows you where something that is leaking, was allocated.

So what it's saying is, after you assigned these values they were never released - which seems like it means your navigation controller is not being released when it should be.

猫瑾少女 2024-09-17 01:43:56

您看到泄漏的对象的唯一引用是否可能在其他也泄漏的对象中?

例如,您在此处分配一个导航控制器:

navigationController = [[UINavigationController alloc] initWithRootViewController:menuView];

当您将其分配给backgroundColor时,该导航控制器将保留UIColor。

您是否在某处释放对该 navigationController 的引用?

Is it possible the only references to the objects you're seeing leaking are in other objects which are also leaking?

For example, you allocate a navigation controller here:

navigationController = [[UINavigationController alloc] initWithRootViewController:menuView];

The UIColor will be retained by that navigationController when you assign it to backgroundColor.

Are you releasing the reference to that navigationController somewhere?

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