简体   繁体   中英

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

@property (nonatomic, copy) NSArray * sourceArray;

I set this array from another class say Class 1 like ...

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

Where I am sure that self.namesArray contains objects.

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...

[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...

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?!

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.

That's the idea, but it may help you to know that -copy often just retains the object for immutable objects. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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