views:

42

answers:

1

I have a ViewController defined as follows:

@interface SectionController : UITableViewController {
   NSMutableArray *sections;
}
- (void) LoadSections;

When LoadSection is call it makes a call to NSURLConnection to load a url which in turn calls

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    [connection release];
    [responseData release];

    NSDictionary *results = [responseString JSONValue];
    NSMutableArray *jSections = [results objectForKey:@"Items"];
    sections = [NSMutableArray array];

    for (NSArray* jSection in jSections)
    {
        Section* section = [Section alloc];
        section.Id = [jSection objectForKey:@"Id"];
        section.Description = [jSection objectForKey:@"Description"];
        section.Image = [jSection objectForKey:@"Image"];
        section.Parent = [jSection objectForKey:@"Parent"];
        section.ProductCount = [jSection objectForKey:@"ProductCount"];
        [sections addObject:section];
        [section release];
    }

    [jSections release];
    [results release];

    [delegate sectionsLoaded];

    [self.view reloadData];
}

The data parses correctly and I now have sections filled with many items.

Calling [self.view reloadData] forces a callback to the delegate method cellForRowAtIndexPath which should then present the data into the cell however its at this point that sections is now nil again.

Can someone please point out my mistake? I must admit I am a newbie to objective c and it probably a pointer issue. What is need to do is retain the value of sections after calling reloadData.

Many thanks.

A: 

Seeing the new code the problem is obvious:

sections = [NSMutableArray array];

should become

[sections release];
sections = [[NSMutableArray alloc] init];

note that the array does not become again "nil", is instead deallocated and you get an invalid reference, which might (should) generate a crash on dereferencing.

I suggest you to read some articles on reference counted memory management as it might be not obvious if you are new to Objective-C, and often leads to mistake (i.e: autorelease is not magic at all)

Michele Balistreri
Sorry I have edited the post to include the real function and I was releasing the section variable, good spot though. The problem still remains that after calling reloadData, sections is nil. It's like it resets the entire viewcontroller.
Chris
I have updated the answer to reflect the new code. I hope it helps
Michele Balistreri
Thanks for the reply but it made no difference.
Chris
Then maybe it would help to post the whole class, and details on how it "doesn't work"
Michele Balistreri
The problem occurs because I release [jSections release]; and [results release];. Is it not good practice to clean up after yourself. It looks like though when I parse the json data it might be passing a pointer and releasing these objects kills that reference? So how can i avoid memory leaks in this case?
Chris
I didn't spot those ones. You do not need to release those actually because you are not retaining them. The rule is simple: if you create the object with alloc/init you need to release it when you are done with it. If you manually retain an object, you need to release it when you are done with it. In every other case, you do not need to do it because other objects (which are responsible by the rules mentioned above) will do it themselves as they are released.
Michele Balistreri
That's great. Thanks so much for your help. I feel my knowledge has moved forward a little this weekend.
Chris