1

I'm using ARC and targeting iOS 5. I've declared an NSMutableArray ivar in one of my view controller's header, initialized it in viewDidLoad, and I'm attempting to manipulate it in some of my view controller's methods. However, it is behaving very weird. As I send messages to it like "addObject:", or try to set it to equal to an NSMutableArray, created in the local scope of a method, it is sending the message to other ivars I've declared.

For example, one of the errors I keep getting is -[PullToRefreshView count]: unrecognized selector sent to instance. PullToRefreshView is another one of my ivars, but in my code, I am clearly sending the count message to my NSMutableArray ivar. When trying to add an object to the ivar NSMutableArray it's count (in debugger) stays at 0. I keep searching my code for something I may have overlooked, but it just seems like some kind of ARC memory management fluke. Any ideas? Here's the relevant areas of my code:

MyViewController.h

@interface MyViewController : UIViewController
{
    PullToRefreshView *pull;
    NSMutableArray *commentsSharesMerged;
}

@property (nonatomic, retain) PullToRefreshView *pull;
@property (nonatomic, retain) NSMutableArray *commentsSharesMerged;

- (void)refreshComments;
- (void)refreshShares;

@end

MyViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    commentsSharesMerged = [[NSMutableArray alloc] init];

    pull = [[PullToRefreshView alloc] initWithScrollView:(UIScrollView *)self.suggestionTable];
    [pull setDelegate:self];
    [self.suggestionTable addSubview:pull];
}

- (void)refreshComments
{
NSURL *url = [NSURL URLWithString:@"http://example.com/comments.json"];
__unsafe_unretained __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

[request setCompletionBlock:^{
    NSString *responseString = [request responseString]; // Use when fetching text data
    NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];

    id jsonObject = [[CJSONDeserializer deserializer] deserialize:responseData error:nil];
    NSLog(@"Connection JSON: %@", jsonObject);

    NSMutableArray *commentsArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < [jsonObject count]; i++)
    {
        NSDictionary *currentCommentData = [jsonObject objectAtIndex:i];

        Comment *comment = [[Comment alloc] init];
        comment.identifier = [[currentCommentData objectForKey:@"id"] intValue];
        comment.firstName = [currentCommentData objectForKey:@"first_name"];
        comment.lastName = [currentCommentData objectForKey:@"last_name"];
        comment.commentText = [currentCommentData objectForKey:@"comment"];

        [commentsArray addObject:comment];
    }

    self.commentsSharesMerged = [commentsArray mutableCopy];

    // I've also tried to add the object using something like this
    //for (Comment *comment in commentsArray)
    //    [commentsSharesMerged addObject:comment];
}];

[request setFailedBlock:^{
    NSError *error = [request error];
    NSLog(@"%@", error);
}];

[request startAsynchronous];
}
  • If you're using ARC, why do you have `retain` in your interface definition? – Stephen Darlington Mar 12 '12 at 10:55
  • try using self.commentsSharesMerged = [[NSMutableArray alloc] init]; – silviupop Mar 12 '12 at 11:02
  • The retain in my interface isn't the issue. In the case of ARC, it is simply a synonym for "strong". It functions the same as using ARC's "strong". I tried initializing commentsSharesMerged with the "self." prefix, but still no luck. :( – Joshua Hensley Mar 12 '12 at 11:11
  • Do you synthesize the properties? If not, show setters and getters, if yes, you don't need to declare the ivars in the interface section, because they are going to be created anyway. You also have a retain cycle on self, check [here](http://stackoverflow.com/questions/4352561/retain-cycle-on-self-with-blocks) for the solution. – lawicko Mar 12 '12 at 13:55
  • Can you show some more code from the implementation? Like the synthesising and also any other places you're using `commentsSharesMerged`. – mattjgalloway Mar 12 '12 at 18:37
  • Thanks for the responses. I did some looking into retain cycle you mentioned. However, if I set a breakpoint in viewDidLoad, when commentsSharedMerged is being initialized, it is not even getting a valid memory address (stays null address). On the next line I'm initializing another NSMutableArray, which is declared in the interface in the same way. It gets initialized properly. So it sounds like the problem exists, long before trying to manipulate commentsSharesMerged in my completionBlock. I am synthesizing the properties. – Joshua Hensley Mar 12 '12 at 19:49
  • On further look...I can see in the debugger that my commentsSharesMerged array is actually being initialized to another ivar. I call self.commentsSharesMerged = [[NSMutableArray alloc] init] and I can see the memory address for one of my other ivars get changed from a null address to a new allocated address. – Joshua Hensley Mar 12 '12 at 21:59

1 Answers1

0

I found the solution for my problem. It can be attributed to some kind of memory bug in LLDB debugging. I moved my scheme over to GDB and that fixed my ivar allocation issues.