Objective-C NSArray NEWBIE Question:

Posted by clearbrian on Stack Overflow See other posts from Stack Overflow or by clearbrian
Published on 2010-03-25T20:46:35Z Indexed on 2010/03/25 20:53 UTC
Read the original article Hit count: 397

Hi I have a weird problems with NSArray where some of the members of the objects in my array are going out of scope but not the others:

I have a simple object called Section. It has 3 members.

@interface Section : NSObject {
    NSNumber *section_Id;   
    NSNumber *routeId;
    NSString *startLocationName;
}
@property(nonatomic,retain)  NSNumber *section_Id;  
@property(nonatomic,retain)  NSNumber *routeId;
@property(nonatomic,retain)  NSString *startLocationName;
@end


@implementation Section

@synthesize section_Id; 
@synthesize routeId;
@synthesize startLocationName;

//Some static finder methods to get list of Sections from the db
+ (NSMutableArray *) findAllSections:{


- (void)dealloc {
    [section_Id release];
    [routeId release];
    [startLocationName release];

    [super dealloc];
}

@end

I fill it from a database in a method called findAllSection

self.sections = [Section findAllSections];

In find all sections I create some local variables fill them with data from db.

NSNumber *secId = [NSNumber numberWithInt:id_section];
NSNumber *rteId = [NSNumber numberWithInt:id_route];
NSString *startName = @"";

Then create a new Section and store these local variable's data in the Section

Section *section = [[Section alloc] init];

section.section_Id = secId;
section.routeId = rteId;
section.startLocationName = startName;

Then I add the section to the array

[sectionsArray addObject:section];

Then I clean up, releasing local variables and the section I added to the array [secId release]; [rteId release]; [startName release]; [locEnd_name release];

[section release];

In a loop repeat for all Sections (release local variables and section is done in every loop)

The method returns and I check the array and all the Sections are there. I cant seem to dig further down to see the values of the Section objects in the array (is this possible)

Later I try and retrieve one of the Sections

I get it from the array

Section  * section = [self.sections objectAtIndex:row];

Then check the value

NSLog(@" SECTION SELECTED:%@",section.section_Id);

BUT call to section.section_Id crashed as section.section_Id is out of scope.

I check the other members of this Section object and theyre ok. After some trial and error I find that by commenting out the release of the member variable the object is OK.

//[secId release];
[rteId release];
[startName release];
[locEnd_name release];


[section release];

My questions are:

Am I cleaning up ok?

Should I release the object added to an array and the local variable in the function?

Is my dealloc ok in Section?

Does this code look ok and should I be looking elsewhere for the problem?

I'm not doing anything complicated just filling array from DB use it in Table Cell.

If its a stupid mistake then tell me (I did put NEWBIE on the question so dont be shocked it was asked)

I can comment out the release but would prefer to know why or if code looks ok then ill need further digging.

the only place that secId is released is in the dealloc.

Thanks

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c