开发者

Unable to get all desired info

Im currently building an app to list all the background processes in the iPhone via the UITableView. However, it is only listing the last processID and processName successfully.

The method for getting the process info is stored in the viewDidLoad method, and looks like this

- (void)viewDidLoad {


int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
size_t miblen = 4;

size_t size;
int st = sysctl(mib, miblen, NULL, &size, NULL, 0);

struct kinfo_proc * process = NULL;
struct kinfo_proc * newprocess = NULL;

do {

    size += size / 10;
    newprocess = realloc(process, size);

    if (!newprocess){

        if (process){
            free(process);
        }


    }

    process = newprocess;
    st = sysctl(mib, miblen, process, &size, NULL, 0);

} while (st == -1 && errno == ENOMEM);

if (st == 0){

    if (size % sizeof(struct kinfo_proc) == 0){
        int nprocess = size / sizeof(struct kinfo_proc);

        if (nprocess){

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

            for (int i = nprocess - 1; i >= 0; i--){


                NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
                NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
                NSString * combinedString = [NSString stringWithFormat:@"%@ %@ %@", processID, @"           " ,processName];

                NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]
                                                                    forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];




                    arryData =  [[NSArray alloc] initWithObjects:[[NSMutableString alloc]initWithFormat:@"%@", combinedString], nil];





                //For debugging purposes
                NSLog(@"%@", dict);


                [processID release];
                [processName release];
      开发者_如何学Python          [array addObject:dict];
                [dict release];
            }

            free(process);
            return [array autorelease];
        }
    }
}



[super viewDidLoad];
}

My table method looks like this :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arryData count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"Process ID              Process Name";
}



// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}


// Set up the cell...
NSString *cellValue= [arryData objectAtIndex:indexPath.row];

cell.text = cellValue;
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
@end

Any help would be very much appreciated! :)


You assign arryData to a new array on every iteration of your for loop. There's no way arryData could contain more than one element. Also you're probably leaking all but the last of those arrays.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜