views:

145

answers:

1

Hello,

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

+2  A: 
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];
    
KennyTM
Yes, that was it. Great to know that. Many thanks!
Jakub