Note that there are some explanatory texts on larger screens.

plurals
  1. POobjective c cocoa updating gui from a separate thread
    primarykey
    data
    text
    <p>I have a thread which is incrementing the value of the variable "int count" . I want to update my UI with the new value of "int count" until I stop the increment by pressing the stop button. I've manage to update the UI but the memory footprint keep on growing. It doesn't show as a memory leak but it is an allocation problem.the heap size is being increased every time there is a call to a UI element from the thread. I can clearly see instruments leaks allocation part that I have some allocations which are only being freed when moving the Window of touching a UI element. I did not manage to solve the problem despite trying everything. If there is a better way to update UI elements with "int count" new value, feel free to let me know. Thanks</p> <p>I posted the link to the cocoa project below if you want to run with instrument or allocation to see the problem or look at the source. It' a small project just a few lines. </p> <p><a href="http://www.mediafire.com/?tqhm248382bjlc3" rel="nofollow">Xcode Poject GUI UPDATE LINK</a></p> <pre><code>-(void) incrementCountGUI:(id)param{ // increment count and update gui NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];// pool just added count++; if (count&gt;=100) {// MAKE SURE COUNT DOESN'T GO ABOVE 100 count=0; } [sliderDisplayOutlet setIntValue:count];// display value of count in textfield [sliderOutlet setIntValue:count];// move slider to value of count [pool release]; } +(void) updateSliderThread:(id)param{// this thread will call incrementCountGUI method to continuously upgrade UI in the background NSAutoreleasePool *myThreadPool=[[NSAutoreleasePool alloc]init]; while (shoudStop==NO) { [ sharedAppInstance performSelectorOnMainThread:@selector(incrementCountGUI:) // update ui in main thread withObject:nil waitUntilDone:NO]; usleep(5000);// sleep microsec; } [myThreadPool release]; } - (IBAction)stopCountAction:(id)sender {// START OR STOP counter thread if ([stopCountButtonOutlet state]==1) { // button depressed=&gt;START shoudStop=NO; [NSThread detachNewThreadSelector:@selector(updateSliderThread:) toTarget:[AppController class] withObject:nil]; [stopCountButtonOutlet setTitle: @" STOP" ]; } if ([stopCountButtonOutlet state]==0){//button depressed=&gt; STOP thread shoudStop=YES; [stopCountButtonOutlet setTitle:@" START INCREMENTING COUNT FROM THREAD "]; } } - (IBAction)sliderAction:(id)sender { // you can change the value of the variable count manualy. count=[sliderOutlet intValue]; [sliderDisplayOutlet setIntValue:count];// display value of count } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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