Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:22:04+00:00 2026-05-26T00:22:04+00:00

I have a thread which is incrementing the value of the variable int count

  • 0

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

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.

Xcode Poject GUI UPDATE LINK

-(void) incrementCountGUI:(id)param{ // increment count and update gui
  NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];// pool just added

    count++;
    if (count>=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=>START
        shoudStop=NO;
        [NSThread detachNewThreadSelector:@selector(updateSliderThread:) toTarget:[AppController class] withObject:nil];
        [stopCountButtonOutlet setTitle: @" STOP" ];

    }
    if ([stopCountButtonOutlet state]==0){//button depressed=> 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


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:22:05+00:00Added an answer on May 26, 2026 at 12:22 am

    1) First of all, you should never update the UI from a thread other than the main thread !

    _Post a notification to the mainThread to ask it to update the UI, or use performSelector:onMainThread: or GCD and get_main_queue(), or whatever solution to make the main thread update the UI.

    [EDIT] Sorry I missed the part of your code where you call performSelectorOnMainThread: so that’s OK.


    2) Moreover, using a thread for what you need is really not necessary. In general you should avoid thread, an prefer other techniques like NSOperationQueues, GCD, or even RunLoop scheduling.

    In your case, using a thread an usleep(5000) is just overhead and will arise a lot of problems related to multithreading (read Apple’s Concurrency Programming Guide and Threading Programming Guide).

    You could do the same thing using a repeating NSTimer, it will be much more easier to code and manage, and will avoid you a lot of trouble.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread which performs some tasks. At the end I want to
I have a thread which drops a circle in the y direction. I want
I have a thread which polls a folder for new files. The problem is
I have a thread which handles all the HTTP Connections to the server and
I have thread exception handler which saves the exception stack trace and should close
I have the main thread from which I start a window using invokeLater .
I have one std::list<> container and these threads: One writer thread which adds elements
I have a parent thread (non-UI) which creates some child threads to do some
I have a parent thread (non-UI) which creates some child threads to do some
I'm working on a thread library which implement user level threads (i have something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.