释放 UIImageView 仍然出现内存泄漏
在将其发布到此处之前,我已尽最大努力进行了搜索。下面是导致内存泄漏的代码。包括自动释放修复了内存泄漏,但我更感兴趣的是知道我在这里做错了什么。如果我拥有它,我应该释放它,这就是我正在尝试做的:) 感谢您提前提供的帮助。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第二行之后,先前分配的welcomeText丢失,并且您正在泄漏该内存。在第一行之后,分配了图像视图,welcomeText 指向该视图。在第二行中分配了另一个图像视图,现在welcomeText 指向这个新图像视图。因此,您没有任何指向第一个分配的图像视图的指针,因此会泄漏。
在这里你实际上不需要第一行。
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 对象。在第二行中,第一行中分配的对象发生泄漏。
You must not allocate an UIImageView object in the first line. In the second line, the object allocated in the first line leaks.