添加到 UIScollView 的 UIImageView 永远不会被释放,内存泄漏?

发布于 2024-12-07 03:44:28 字数 2032 浏览 0 评论 0原文

这是我的第一篇文章,你是我最后的希望。

我的 iPad 应用程序中有一个包含图像的列表,如果您选择一个,则会加载并显示扩展 UIViewController 的类 MyViewController(只需设置 window.rootViewController,通过使用 UINavigationController,通过 presentModalViewController - 尝试了一切,没有任何区别)。它内部有一个 UIScrollView,它使用以下代码将图像的大版本添加到 UIScrollView

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[[delegate getImageNames] objectAtIndex:(gameNr*2)]]];
tempImageView.frame = CGRectMake(0,0,tempImageView.frame.size.width/2,tempImageView.frame.size.height/2);
[self setMyImage:tempImageView];
[tempImageView release];

myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
myScrollView.maximumZoomScale = 2.0;
myScrollView.minimumZoomScale = 1.0;
myScrollView.clipsToBounds = YES;
myScrollView.bounces = NO;
myScrollView.delegate = self;
[myScrollView addSubview: myImage];

myImage 是一个 (nonatomic , keep) 类型的 UIImageView 属性在我的 MyViewController 中。

按下按钮并返回到图像列表后,我释放对 MyViewController 的引用,它的 dealloc 方法的调用如下:

- (void)dealloc
{
    CFShow(@"dealloc!");
    for(UIImageView *subview in [myScrollView subviews]) {
        if(subview.frame.size.width > 20){
            [subview removeFromSuperview];
            [subview setImage:nil];
        }
    }

    [myImage setImage:nil];

    self.myScrollView = nil;
    self.myImage = nil;

    [myScrollView release];
    [myImage release];
    [super dealloc];
}

到目前为止,它有效,但问题是,图像并没有真正以这种方式释放。打开和关闭大约 12 个图像后,应用程序崩溃并出现内存警告。 我从这里得到的report_memory方法也证实了这一点: iOS 低内存崩溃,但内存使用率非常低

调用 dealloc 方法,进行三次检查。 我通过调试器中的断点检查了变量 myImagemyScrollView,它们被 dealloc 方法中的这些命令设置为 0x0。为什么内存没有释放???????

感谢您的任何建议,我整整三天都在研究这个问题,这让我发疯。

This is my first post and you are my last hope.

I have a list with images in my iPad app, if you select one, my class MyViewController which extends UIViewController is loaded and shown (by just setting window.rootViewController, by using UINavigationController, by presentModalViewController - tried everything, doesn't make any difference). It has a UIScrollView inside, which adds a big version of the image to the UIScrollView with this code:

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[[delegate getImageNames] objectAtIndex:(gameNr*2)]]];
tempImageView.frame = CGRectMake(0,0,tempImageView.frame.size.width/2,tempImageView.frame.size.height/2);
[self setMyImage:tempImageView];
[tempImageView release];

myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
myScrollView.maximumZoomScale = 2.0;
myScrollView.minimumZoomScale = 1.0;
myScrollView.clipsToBounds = YES;
myScrollView.bounces = NO;
myScrollView.delegate = self;
[myScrollView addSubview: myImage];

myImage is a (nonatomic, retain) property of the type UIImageView inside my MyViewController.

After pressing a button and go back to the list of images, I release the reference to MyViewController and it's dealloc method is called like this:

- (void)dealloc
{
    CFShow(@"dealloc!");
    for(UIImageView *subview in [myScrollView subviews]) {
        if(subview.frame.size.width > 20){
            [subview removeFromSuperview];
            [subview setImage:nil];
        }
    }

    [myImage setImage:nil];

    self.myScrollView = nil;
    self.myImage = nil;

    [myScrollView release];
    [myImage release];
    [super dealloc];
}

It works so far, but the problem is, that the memory of the images is not really deallocated this way. After opening and closing about 12 images, the app crashes with memory warning.
This is also confirmed by the report_memory method I got from here:
iOS Low Memory Crash, but very low memory usage

The dealloc method IS called, that's triple checked.
I checked the variables myImage and myScrollViewvia breakpoint in the debugger, they are set to 0x0 by those commands in the dealloc method. Why is the memory not freed????????

Thanks for any suggestion, I am working on this since three whole days, it's driving me crazy.

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

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

发布评论

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

评论(1

七秒鱼° 2024-12-14 03:44:28

将其替换

self.myScrollView = nil;
self.myImage = nil;

[myScrollView release];
[myImage release];

为:

[myScrollView release];

self.myScrollView = nil;
self.myImage = nil;

并发布定义属性 myScrollViewinit 的代码。

并且您不需要循环 for(UIImageView *subview in [myScrollView subviews])。当您释放 myScrollView 对象时,它将自动释放其所有子视图。

Replace this:

self.myScrollView = nil;
self.myImage = nil;

[myScrollView release];
[myImage release];

with this:

[myScrollView release];

self.myScrollView = nil;
self.myImage = nil;

And post code where you define property myScrollView and init it.

And you don't need loop for(UIImageView *subview in [myScrollView subviews]). When you will release myScrollView object it will automatically release all its subviews.

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