Note that there are some explanatory texts on larger screens.

plurals
  1. POexecution stops on [textView insertText:] without exception!
    text
    copied!<p>I am writing a very simple OSX app. In this app, there is a main thread that keeps updating some stuff, then calculates some statistics and prints them on a textView.</p> <p>While developing, i used the same received IBAction to perform this cycle. I got it all working, then switched to NSThread to prevent the UI from locking while computing.</p> <p>As soon as i did that, the app started running very few cycles (about 7-8), then the whole app freezes without any exception. By debugging, i noticed that it freezes when trying to print statistics on the textView, and i have absolutely no clue about how to solve this. It works if not inside a thread... Anyone can help? Code below. Thanks in advance :)</p> <pre><code>-(IBAction) Evolve:(id)sender{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [NSThread detachNewThreadSelector:@selector(Evolve) toTarget:self withObject:nil]; [pool drain]; } </code></pre> <p>And this is the whole cycle</p> <pre><code>-(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]; for (int i = 0; i &lt; 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&lt;maxGen &amp;&amp; !earlyBestFound) { NSLog(@"while"); NSArray *parents = [env selectParents]; NSLog(@"parents selected"); NSMutableArray *offspring = [[NSMutableArray alloc] init]; for (int i = 0; i &lt; [parents count]; i+=2) { if (i+1&lt;[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] &gt; [parent1 fitness] &amp;&amp; tagMinMax == 0)||([child1 fitness] &lt; [parent1 fitness] &amp;&amp; tagMinMax == 1)) { child1 = parent1; } if (([child2 fitness] &gt; [parent2 fitness] &amp;&amp; tagMinMax == 0)||([child2 fitness] &lt; [parent2 fitness] &amp;&amp; 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] &gt; best &amp;&amp; tagMinMax == 1)||([X fitness] &lt; best &amp;&amp; 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 some 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]; } </code></pre> <p>P.S. Sorry if the english's not perfect, i'm italian :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload