在其他函数中保留 count = 0 吗?内存管理问题?

发布于 2024-09-05 03:10:23 字数 492 浏览 2 评论 0原文

我在头文件中声明了一个 NSMutableArray :

NSMutableArray *myMuArr;

@property (nonatomic, retain) NSMutableArray *myMuArr;

.m 文件中,我得到了来自其他类的委托:

-(void)didGrabData:(NSArray*)theArray {
self.myMuArr = [[[NSMutableArray alloc] initWithArray:myMuArr]retain];
}

如果我想访问 cellForRowAtIndexPath 中的 self.myMuArr 它是空的(我检查了数组的保留计数是 0)

我做错了什么?

当然它是在dealloc中释放的,而不是在其他地方。

我将非常感谢任何帮助:0)

I declared a NSMutableArray in the header-file with:

NSMutableArray *myMuArr;

and

@property (nonatomic, retain) NSMutableArray *myMuArr;

In the .m file I've got a delegate from an other class:

-(void)didGrabData:(NSArray*)theArray {
self.myMuArr = [[[NSMutableArray alloc] initWithArray:myMuArr]retain];
}

If I want to access the self.myMuArr in cellForRowAtIndexPath it's empty (I checked the retain count of the array and it's 0)

What am I doing wrong?

Of course it's released in the dealloc, no where else.

I would be very thankfull for any help :0)

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

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

发布评论

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

评论(2

︶ ̄淡然 2024-09-12 03:10:23

我认为你不明白何时保留和释放。看看这个问题和答案。通读并理解它并阅读它链接到的文章。这会对你有很大帮助。在 iPhone 上了解这一点非常重要。

你的数组被保留了 3 次,但你只需要一次:
(1) 当你调用alloc/init时,你得到一个你拥有的数组(它被保留)。
(2) 当你调用retain时,那就是额外的retain。
(3) 您的 setter self.myMuArr 被定义为保留在 @property 中,因此当您使用 setter 方法分配指针时,它会再次保留。

您通常会看到代码使用这两种技术,两者都是正确的:

-(void)didGrabData:(NSArray*)theArray {
   NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:obj1, obj2, nil];
   self.myMutArr = arr;
   [arr release];       
}

或者使用 arrayWithObjects,它为您提供一个自动释放的数组,如果您想获得其内存空间的所有权并保留它,则需要保留该数组。你的设置者会这样做:

-(void)didGrabData:(NSArray*)theArray {
   self.myMutArr = [NSMutableArray arrayWithObjects:obj1, obj2, nil];
}

I don't think you understand when to retain and release. Take a look at this SO question and answer. Read it thru and understand it and the read the articles it links to. It will help you a lot. It is essential to understand this on the iPhone.

Your array is getting retained 3 times, but you only needed one:
(1) When you call alloc/init, you get an array that you own (it is retained).
(2) When you called retain, that's an extra retain.
(3) Your setter self.myMuArr was defined to retain in the @property, so when you assign the pointer using the setter method it gets retained again.

You commonly see code use these two techniques, both are correct:

-(void)didGrabData:(NSArray*)theArray {
   NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:obj1, obj2, nil];
   self.myMutArr = arr;
   [arr release];       
}

Or using arrayWithObjects, which gives you an autoreleased array that you need to retain if you want to take ownership of its memory space and keep it around. Your setter will do that:

-(void)didGrabData:(NSArray*)theArray {
   self.myMutArr = [NSMutableArray arrayWithObjects:obj1, obj2, nil];
}
土豪我们做朋友吧 2024-09-12 03:10:23

如果不是因为您尝试在数组存在之前使用其自身内容来初始化数组,那么您最终的保留计数应该为 3。 行替换这行代码:

self.myMuArr = [[[NSMutableArray alloc] initWithArray:myMuArr]retain];

尝试用以下

self.myMuArr = [NSMutableArray arrayWithArray:theArray];

You should end up with a retain count of 3 if it wasn't for the fact that you try to initialize the array with the content of itself before it exists. Try replacing this line of code:

self.myMuArr = [[[NSMutableArray alloc] initWithArray:myMuArr]retain];

with this line:

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