开发者

Message sent to deallocated instance which has never been released

开发者 https://www.devze.com 2022-12-26 16:35 出处:网络
I started dealing with NSOperations and (as usual with concurrency) I\'m observing strange behaviour.

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:

  1. postResultsArray = [[NSMutableArray array] retain];
    
  2. postResultsArray = [[NSMutableArray alloc] init];
    
  3. Exploit the fact that a setter should retain the new value (if you declare as @property(retain)):

    self.postResultsArray = [NSMutableArray array];
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号