我有一個線程正在遞增變量「int count」的值。我想用新的「int count」值更新我的UI,直到我按停止按鈕停止增量。我已經設法更新用戶界面,但內存佔用不斷增長。它不會顯示爲內存泄漏,但它是一個分配問題。每次調用線程中的UI元素時,堆大小都會增加。我可以清楚地看到儀器泄漏分配部分,我有一些分配只有在移動觸摸UI元素的窗口時纔會被釋放。儘管嘗試了一切,我仍然無法解決問題。 如果有更好的方法用「int count」新值更新UI元素,請隨時通知我。 謝謝objective c cocoa從單獨的線程更新gui
我發佈了下面的可可項目的鏈接,如果你想運行儀器或分配來查看問題或查看源代碼。這是一個只有幾行的小型項目。所有的
-(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
}
爲什麼要創建一個自動釋放池,如果你不會耗盡它? – bryanmac
@bryanmac新線程的標準練習。所有的線程方法都應該有一個autorelease池。你如何知道performSelectorOnMainThread:不會將任何autorelease'd對象添加到當前池中? –
我添加了一個autorelease池到我的方法「incrementCountGui」,但它沒有幫助。堆大小不斷增加我甚至可以在活動監視器上看到它 –