objective c - execution stops on [textView insertText:] without exception! -


i writing simple osx app. in app, there main thread keeps updating stuff, calculates statistics , prints them on textview.

while developing, used same received ibaction perform cycle. got working, switched nsthread prevent ui locking while computing.

as did that, app started running few cycles (about 7-8), whole app freezes without exception. debugging, noticed freezes when trying print statistics on textview, , have absolutely no clue how solve this. works if not inside thread... can help? code below. in advance :)

-(ibaction) evolve:(id)sender{  nsautoreleasepool *pool = [[nsautoreleasepool alloc] init];  [nsthread detachnewthreadselector:@selector(evolve) totarget:self withobject:nil];  [pool drain]; } 

and whole cycle

-(void) evolve{      nsautoreleasepool *pool = [[nsautoreleasepool alloc] init];      srandom(time(null));      //tag 0: minimo, tag 1: massimo     int tagminmax = [minmax selectedtag];      //tag 0: rimpiazza sempre, tag 1: rimpiazza solo se migliori     int tagrimpiazzo = [[rimpiazzo selecteditem] tag];      int popnum = [tf_popnum intvalue];     int maxgen = [tf_maxgen intvalue];     int target = [tf_targetval intvalue];     int chromosomelength = [tf_chromosomelength intvalue];      environment *env = [[environment alloc] init];      nsmutablearray *pop = [[nsmutablearray alloc] init];     (int = 0; < popnum; i++) {         [pop addobject:[[individual alloc] initwithrandomgeneswithchromosomelength:chromosomelength]];     }      [env setpopulation:pop];     [pop release];      bool earlybestfound = no;     individual *earlybest = nil;     int i=0;     float best, avg;     while (i<maxgen && !earlybestfound) {         nslog(@"while");         nsarray *parents = [env selectparents];         nslog(@"parents selected");         nsmutablearray *offspring = [[nsmutablearray alloc] init];         (int = 0; < [parents count]; i+=2) {             if (i+1<[parents count]) {                 nslog(@"beginning sex");                 individual *parent1 = [parents objectatindex:i];                 individual *parent2 = [parents objectatindex:i+1];                  nsarray *children = [parent1 kcrossoverwithotherindividual:1 individual:parent2];                  individual *child1 = [children objectatindex:0];                 individual *child2 = [children objectatindex:1];                  nslog(@"children born");                  if (tagrimpiazzo!=0) {                     if (([child1 fitness] > [parent1 fitness] && tagminmax == 0)||([child1 fitness] < [parent1 fitness] && tagminmax == 1)) {                         child1 = parent1;                     }                     if (([child2 fitness] > [parent2 fitness] && tagminmax == 0)||([child2 fitness] < [parent2 fitness] && tagminmax == 1)) {                         child2 = parent2;                     }                 }                  nslog(@"replacement happened");                  [offspring addobject:child1];                 [offspring addobject:child2];             }         }         nslog(@"calculating statistics");         avg = 0;         for(individual *x in offspring){              if (([x fitness] > best && tagminmax == 1)||([x fitness] < best && tagminmax == 0)) {                 best = [x fitness];             }              avg += [x fitness];              if ([x fitness]==target) {                 earlybestfound = yes;                 earlybest = x;             }         }         avg = avg/(float)popnum;          [env setpopulation:offspring];          nslog(@"releasing memory");          [offspring release];          nslog(@"printing statistics");         nsstring *toprint = [nsstring stringwithformat:@"fine generazione: %d avg: %.2f best: %.2f \r\n", i,avg,best];         [textview inserttext:toprint];          i++;     }      nslog(@"fine");      nsstring *toprint = [nsstring stringwithformat:@"fine! best: %.2f - avg: %.2f \r\n", i,avg,best];     [textview inserttext:toprint];      [env release];      [pool drain]; } 

p.s. sorry if english's not perfect, i'm italian :)

your application crashing because accessing textview background thread. ui objects may accessed main thread.

to solve problem, need forward textview updates main ui thread. can using -performselectoronmainthread: method. example:

[textview performselectoronmainthread:@selector(inserttext:)                             withobject:toprint                          waituntildone:yes]; 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -