Message sent to deallocated instance which has never been released
I started dealing with NSOperations and (as usual with concurrency) I'm observing strange behaviour. In my class I've got an instance variable:
NSMutableArray *postResultsArray;
when one button in the UI is pressed I initialize the array:
postResultsArray = [NSMutableArray array];
and setup the operations (together with dependencies).
In the operations I create a custom object and try to 开发者_如何学Pythonadd to the array:
PostResult *result = [[PostResult alloc] initWithServiceName:@"Sth" andResult:someResult];
[self.postResultsArray addObject:result];
and while adding I get:
-[CFArray retain]: message sent to deallocated instance 0x3b40c30
which is strange as I don't release the array anywhere in my code (I did, but when the problem started to appear I commented all the release operations to be sure that they are not the case). I also used to have @synchronized section like below:
PostResult *result = [[PostResult alloc] initWithServiceName:@"Sth" andResult:someResult];
@synchronized (self.postResultsArray) {
[self.postResultsArray addObject:result];
}
but the problem was the same (however, the error was for the synchronized operation).
Any ideas what I may be doing wrong?
postResultsArray = [NSMutableArray array];
[NSMutableArray array]
is a convient method which is equivalent to [[[NSMutableArray alloc] init] autorelease]
. So there is an implicit (auto)release there. Since you're going to keep it, you have to use one of these 3 changes:
postResultsArray = [[NSMutableArray array] retain];
postResultsArray = [[NSMutableArray alloc] init];
Exploit the fact that a setter should retain the new value (if you declare as
@property(retain)
):self.postResultsArray = [NSMutableArray array];
精彩评论