释放 UIImageView 仍然出现内存泄漏

发布于 2024-10-27 23:37:43 字数 718 浏览 2 评论 0原文

在将其发布到此处之前,我已尽最大努力进行了搜索。下面是导致内存泄漏的代码。包括自动释放修复了内存泄漏,但我更感兴趣的是知道我在这里做错了什么。如果我拥有它,我应该释放它,这就是我正在尝试做的:) 感谢您提前提供的帮助。 ContainerView 是一个 UIView,我正在向其中添加欢迎文本。

UIImageView *welcomeText = [[UIImageView alloc] init];
welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero];
welcomeText.frame = (CGRect) {  CGRectGetMidX(containerView.frame) -295 , 100.0, 590,134 };
welcomeText.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | 
        UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
welcomeText.image = [UIImage imageNamed:@"welcome-to-the-jungle.png"];
[containerView addSubview:welcomeText];
[welcomeText release];

I have searched my best before posting this here. below is the code that is causing memory leak. Including autorelease fixes the memory leak but I am more interested in knowing what am I doing wrong here. If i own it, I should release it, which is what I am trying to do :)
THankyou for the help in advance. ContainerView is a UIView, and I am adding my welcome text to it.

UIImageView *welcomeText = [[UIImageView alloc] init];
welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero];
welcomeText.frame = (CGRect) {  CGRectGetMidX(containerView.frame) -295 , 100.0, 590,134 };
welcomeText.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | 
        UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
welcomeText.image = [UIImage imageNamed:@"welcome-to-the-jungle.png"];
[containerView addSubview:welcomeText];
[welcomeText release];

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

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

发布评论

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

评论(2

三生池水覆流年 2024-11-03 23:37:43
UIImageView *welcomeText = [[UIImageView alloc] init];
welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero];

在第二行之后,先前分配的welcomeText丢失,并且您正在泄漏该内存。在第一行之后,分配了图像视图,welcomeText 指向该视图。在第二行中分配了另一个图像视图,现在welcomeText 指向这个新图像视图。因此,您没有任何指向第一个分配的图像视图的指针,因此会泄漏。

在这里你实际上不需要第一行。

UIImageView *welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero];
UIImageView *welcomeText = [[UIImageView alloc] init];
welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero];

After the 2nd line previously alloced welcomeText is lost and you are leaking that memory. After 1st line an image view is alloced and welcomeText is pointing to that. In 2nd line another image view is alloced and now welcomeText is pointing to this new one. So you don't have any pointer to 1st alloced image view and as a result that is leaked.

Here you don't need the 1st line actually.

UIImageView *welcomeText = [[UIImageView alloc] initWithFrame:CGRectZero];
情魔剑神 2024-11-03 23:37:43

您不得在第一行分配 UIImageView 对象。在第二行中,第一行中分配的对象发生泄漏。

You must not allocate an UIImageView object in the first line. In the second line, the object allocated in the first line leaks.

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