Objective-c - 当我将视图更改为零时保留计数

发布于 2024-10-06 21:01:52 字数 782 浏览 0 评论 0原文

我在一个视图控制器中声明了一个 UIImage 和一个 UIImage View,如下所示:

在 .h 文件中:

UIImageView* itemImageView;
UIImage* itemImage;

@property (nonatomic, retain) UIImage* itemImage;
@property (nonatomic, retain) UIImageView* itemImageView;

在 .m 文件中:

@synthesize itemImage, itemImageView;

在另一个视图中,我设置了它的值:

UIImage *image = [UIImage imageNamed:@"name1.png"];
imgView.itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 166)];
imgView.itemImageView.image = image;

[self.parentViewController.view addSubview:imgView.itemImageView];
[self dismissModalViewControllerAnimated:YES];  

在这个方法中,itemImageView 的保留计数是 2。

但是当我返回到我放置属性和合成的视图,保留计数为 0,我无法访问该对象。

知道发生了什么吗?

I declared a UIImage and a UIImage View in one viewcontroller like this:

In the .h file:

UIImageView* itemImageView;
UIImage* itemImage;

@property (nonatomic, retain) UIImage* itemImage;
@property (nonatomic, retain) UIImageView* itemImageView;

In the .m file:

@synthesize itemImage, itemImageView;

In another view, I set its value:

UIImage *image = [UIImage imageNamed:@"name1.png"];
imgView.itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 166)];
imgView.itemImageView.image = image;

[self.parentViewController.view addSubview:imgView.itemImageView];
[self dismissModalViewControllerAnimated:YES];  

Inside this method, the retain count of itemImageView is 2.

But when I go back to the view where I put the property and the synthesize, the retain count is 0 and I cannot access the object.

Any idea whats happening?

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

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

发布评论

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

评论(3

末骤雨初歇 2024-10-13 21:01:52

你这里的代码看起来没问题。 (除了内存泄漏)您将一个带有保留计数+1的ImageView分配给itemImageView,这会将其增加到2。将 ImageView 设置为 itemImageView 后,您需要在 ImageView 上调用release:

UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 166)];
imgView.itemImageView = iv;
[iv release];

但是,这并不能解决您的问题(甚至会使情况变得更糟..)
你能展示更多代码吗?您是否尝试过使用调试器单步执行它?

Your code here looks ok. (Aside from a memory leak) You're assigning an ImageView with a retain-count +1 to itemImageView, which will increase it to two. You need to call release on your ImageView after setting it to itemImageView:

UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 166)];
imgView.itemImageView = iv;
[iv release];

However, this doesn't fix your problem (it will even make it worse..)
Can you show more code? Have you tried stepping through it with the Debugger?

深者入戏 2024-10-13 21:01:52
UIImageView* itemImageView;
...
@property (nonatomic, retain) UIImageView* itemImageView;
...
imgView.itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 166)];

这会导致双重保留,因为 alloc 将保留计数器增加 1,@propertyretain 设置也是如此。

设置属性的一般模式如下:

.h:

SomeClass* someClass;
...
@property (nonatomic, retain) SomeClass* someClass;

.m:

SomeClass* temporarySomeClass = [[SomeClass alloc] init];
self.someClass = temporarySomeClass;
[temporarySomeClass release];
...

这里,我们使用临时变量来保存我们分配的对象,然后立即释放。

您肯定会在 Apple 示例代码中看到这一点。

UIImageView* itemImageView;
...
@property (nonatomic, retain) UIImageView* itemImageView;
...
imgView.itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 82, 166)];

This causes a double retain since alloc increases the retain counter by 1, and so does the retain setting of the @property.

The general pattern for setting properties is like so:

.h:

SomeClass* someClass;
...
@property (nonatomic, retain) SomeClass* someClass;

.m:

SomeClass* temporarySomeClass = [[SomeClass alloc] init];
self.someClass = temporarySomeClass;
[temporarySomeClass release];
...

Here, we're using a temporary variable to hold the object we alloced, and then doing a release right after.

You'll see that in the Apple example code for sure.

好久不见√ 2024-10-13 21:01:52

你的问题在于这一行:

[self.parentViewController.view addSubview:imgView.itemImageView];

这很可能应该这样编辑:

[self.parentViewController.view addSubview:self.parentViewController.itemImageView];

这导致了 imgView 是否真的等同于parentViewController.view 的问题。如果你想要它,那么你需要弄清楚它被分配到哪里,并看看你在哪里搞砸了。如果不是,那么除了在分配它之前构建对象的临时容器之外,将其用于任何其他用途都是没有意义的。

编辑:内存泄漏是一个单独的问题是的,但我不确定你如何在方法中声明 imgView 所以我把它留给你解决:)

your problem is with this line:

[self.parentViewController.view addSubview:imgView.itemImageView];

this should most likely be edited as such:

[self.parentViewController.view addSubview:self.parentViewController.itemImageView];

this leads to the question of whether imgView is really equivalent to parentViewController.view. if you wanted it to be, then you need to figure out where that got assigned and see where you messed that up. if not, then theres no point in using it for anything but a temporary container to build ur objects in before assigning it.

edit: the memory leak is a separate issue yes, but im not sure how u declared imgView in the method so i left that for you to solve :)

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