Search code examples
arrayscocoafunctionvariablesobjective-c-2.0

Accessing an array by index passed by function call


I have written a specific init-override-function where i want to pass an index number to be called in an array. The index number itself is defined by selecting a tablerow in a tableview by the user. So.. the rownumber that is selected shall be passed into the init function and used there for further processing.

Well.. now there is my problem.. In my opinion the methods I've created are all correctly coded. But when I click my defined Connect Button an error message appears in the console, that, the index is out of bounds. So.. I've checked the array for entries and there are all available. So the indexnumber should be fine.

Maybe fyi: I've created a copy of the array in the TableViewController that is originally located in the PortConnection file.

Here are the necessary files. Can anyone give me a helping hand, where to search for?

PORTTABLEVIEWCONTROLLER.M

- (IBAction)pushConnect:(id)sender {
    NSInteger selectedRow = [tableView selectedRow];
    [portConnection initPort:selectedRow];
}

- (id)init {
    self = [super init];
    if (self) {
        // Initialization of port Connection instance
        portConnection = [[PortConnection alloc] init];
        // Fill array in Portconnection.m with devices
        [portConnection listDevices];
        // Fill tableView Data Array with data from portConnection array
        self.tableViewDataArray = [NSMutableArray arrayWithArray:portConnection.portArray];
    }
    return self;
}

PORTCONNECTION.H

@

interface PortConnection : NSObject {
    // Instance of AMSerialPort
    AMSerialPort *port;

    // Port Array to be filled with found ports
    NSMutableArray *portArray;
}

// List Devices into an given array
- (void)listDevices;

// Connect to selected port
- (void)initPort:(NSInteger)selectedRow;

@property (nonatomic, retain) NSMutableArray *portArray;
@property (nonatomic, retain) AMSerialPort *port;
@end

PORTCONNECTION.M

@implementation PortConnection

@synthesize port;
@synthesize portArray;

#pragma mark -
#pragma mark Serial Port Access

- (void)listDevices {

    // get an port enumerator
    NSEnumerator *enumerator = [AMSerialPortList portEnumerator];
    AMSerialPort *aPort;

    while ((aPort = [enumerator nextObject])) {
        [portArray addObject:[PortItem portItemWithTitle:[aPort name] andPath:[aPort bsdPath]]];
    }
}

- (void)initPort:(NSInteger)selectedRow {
    //Create object of selected port searched in array
    PortItem *portSelected = [portArray objectAtIndex:selectedRow];
    NSString *deviceNameSelected = [portSelected valueForKey:@"bsdPath"];

    // Start Connection
    if (![deviceNameSelected isEqualToString:[self.port bsdPath]]) {
        [self.port close];
        [self setPort:[[[AMSerialPort alloc] init:deviceNameSelected withName:deviceNameSelected type:(NSString *)CFSTR(kIOSerialBSDModemType)] autorelease]];
        [self.port setDelegate:self.port];

        if ([self.port open]) {
            NSLog(@"Connected...");
            [self.port setSpeed:B38400];
            [self.port readDataInBackground];
        } else {
            NSLog(@"error connecting");
            [self setPort:nil];
        }
    }
}

#pragma mark -
#pragma mark initialization / deallocation

- (id)init {
    self = [super init];
    if (self) {
        portArray = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc {
    portArray = NULL;
    [super dealloc];
}

@end

Well.. my idea is, that something is wrong with the method INITPORT:(NSINTEGER)SELECTEDROW but I am not sure at all....

Thanks so much for giving me advice again!

Sebastian


Solution

  • Your problem is this line,

    portArray = [NSMutableArray array];
    

    Although it is a retained variable, it will retain the value when you use the property setter method. This is a direct assignment of an autoreleased object will be released in a while and baring any other object retaining it (which doesn't happen), it should get deallocated. This is something which you don't want. Remedy this by using the property setter,

    self.portArray = [NSMutableArray array];