简体   繁体   English

释放复制的对象时执行EXEC_BAD_ACCESS

[英]EXEC_BAD_ACCESS when releasing a copied object

This has been killing me.. Because its a memory management issue... 这一直让我丧命。.因为它的内存管理问题...

I have a NSArray created like so in say Class 2 我有一个创建的NSArray,就像在Class 2中那样

@property (nonatomic, copy) NSArray * sourceArray;

I set this array from another class say Class 1 like ... 我从另一个类(例如Class 1)设置此数组...

Class2 = [[Class2 alloc] initWithFrame:self.bounds];
[Class2 setSourceArray:self.namesArray];

Where I am sure that self.namesArray contains objects. 我确定self.namesArray包含对象。

When I release Class 1, it releases Class 2 since Class 2 is a subview in Class 1 which is expected, but I get an EXEC_BAD_ACCESS when Class 2 releases sourceArray in dealloc like so... 当我释放Class 1时,它会释放Class 2,因为Class 2是Class 1中的子视图,但是当Class 2在dealloc中释放sourceArray时,我得到了EXEC_BAD_ACCESS,就像这样...

[sourceArray release];

I DO NOT get this error if I do not release namesArray in Class 1.. Which doesn't make sense because I am using I declared sourceArray as COPY which to my knowledge gives Class 2 its own version of the array... 如果我不释放Class 1中的namesArray,则不会出现此错误。这没有任何意义,因为我使用的是声明sourceArray为COPY,据我所知,它为Class 2提供了自己的数组版本。

Can anyone help me here? 有人能帮我一下吗? Its killing me! 这太痛苦了!

More info: The reference count right before I try to release sourcearray is 1... So Why would a release not work?! 更多信息:在我尝试发布sourcearray之前的引用计数为1 ...那么为什么发布不起作用?

Which doesn't make sense because I am using I declared sourceArray as COPY which to my knowledge gives Class 2 its own version of the array. 这没有意义,因为我使用的是将sourceArray声明为COPY,据我所知,它为Class 2提供了自己的数组版本。

That's the idea, but it may help you to know that -copy often just retains the object for immutable objects. 这就是这个主意,但是它可能会帮助您了解-copy通常只是将对象保留为不可变的对象。 That shouldn't matter to your code, since the original is indistinguishable from a copy for immutable objects. 这与您的代码无关紧要,因为原始对象与不可变对象的副本没有区别。 It may be that you're over-releasing the array in Class1, and the problem only shows up when Class2 releases it's "copy" of the array. 可能是您过度释放了Class1中的数组,而该问题仅在Class2释放数组的“副本”时才会出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM