Che*_*tan 1 crash cocoa objective-c
好的,这个问题变得越来越复杂,所以我要把它完全划掉并重新开始.我设法创建了一个非常基本的Cocoa应用程序来演示我的问题.
所以这就是问题所在:
在线程内部,我在应用程序的状态栏菜单中设置菜单项的标题.当应用程序尝试更改菜单项的标题时,我打开了菜单,大部分时间它都可以工作,但有时会崩溃应用程序.
以下是示例应用的完整代码:
MenubarFailAppDelegate.h
#import <Cocoa/Cocoa.h>
@interface MenubarFailAppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
IBOutlet NSMenuItem *menuItem;
int currentDelay;
}
@end
Run Code Online (Sandbox Code Playgroud)
MenubarFailAppDelegate.m
#import "MenubarFailAppDelegate.h"
@implementation MenubarFailAppDelegate
- (id)init {
self = [super init];
if (self != nil)
{
currentDelay = 0;
}
return self;
}
- (void)awakeFromNib {
// Create status menu item
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setMenu:statusMenu];
[statusItem setTitle:@"Fail"];
[statusItem setHighlightMode:YES];
currentDelay = 3;
[NSThread detachNewThreadSelector:@selector(changeStatusItemTitleLoopThread)
toTarget:self
withObject:nil];
}
- (void)changeStatusItemTitleLoopThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while (currentDelay > 0) {
// Sleep for one second
NSDate *future = [NSDate dateWithTimeIntervalSinceNow:1];
[NSThread sleepUntilDate:future];
// Decrement current delay
currentDelay--;
}
NSLog(@"Setting menu item title");
[menuItem setTitle:@"lol cat"];
[pool release];
}
@end
Run Code Online (Sandbox Code Playgroud)
以下是在记录"设置菜单项标题"后程序崩溃时出现的崩溃报告:
#0 0x7fff870f74c0 in HIStandardMenuView::FetchItemCache
#1 0x7fff870f6345 in HIStandardMenuView::GetOptimalSizeSelf
#2 0x7fff870f612b in HIView::GetOptimalSizeWithLimitsSelf
#3 0x7fff870f5fb5 in HIView::SendGetOptimalBounds
#4 0x7fff870f5edb in HIView::GetOptimalSize
#5 0x7fff870f5dc1 in HandleCalculateMenuSize
#6 0x7fff8709b600 in MenuData::EventHandler
#7 0x7fff87093997 in DispatchEventToHandlers
#8 0x7fff87092ee6 in SendEventToEventTargetInternal
#9 0x7fff87092d57 in SendEventToEventTargetWithOptions
#10 0x7fff870f5b7a in _CalcMenuSizeOnDevice
#11 0x7fff8710811c in RecalcAndResizeMenu
#12 0x7fff8720efa6 in ResizeMenuArray
#13 0x7fff8720eff6 in ResizeOpenMenus
#14 0x7fff87093997 in DispatchEventToHandlers
#15 0x7fff87092ee6 in SendEventToEventTargetInternal
#16 0x7fff87092d57 in SendEventToEventTargetWithOptions
#17 0x7fff870c012a in ToolboxEventDispatcherHandler
#18 0x7fff87093d91 in DispatchEventToHandlers
#19 0x7fff87092ee6 in SendEventToEventTargetInternal
#20 0x7fff870b0ba9 in SendEventToEventTarget
#21 0x7fff870bdeed in AcquireEventFromQueue
#22 0x7fff870ba737 in ReceiveNextEventCommon
#23 0x7fff87104db8 in IsUserStillTracking
#24 0x7fff870f1a48 in TrackMenuCommon
#25 0x7fff87215ac9 in PopUpMenuSelectCore
#26 0x7fff87215dce in _HandlePopUpMenuSelection7
#27 0x7fff864d71c9 in _NSSLMPopUpCarbonMenu3
#28 0x7fff86706e71 in -[NSStatusBarButtonCell trackMouse:inRect:ofView:untilMouseUp:]
#29 0x7fff8640a4b5 in -[NSControl mouseDown:]
#30 0x7fff86324763 in -[NSWindow sendEvent:]
#31 0x7fff86707c10 in -[NSStatusBarWindow sendEvent:]
#32 0x7fff86259ee2 in -[NSApplication sendEvent:]
#33 0x7fff861f0922 in -[NSApplication run]
#34 0x7fff861e95f8 in NSApplicationMain
#35 0x100001499 in main at main.m:13
Run Code Online (Sandbox Code Playgroud)
通常不建议在线程代码中进行UI更改.是否有令人信服的理由需要更改线程中的菜单标题?我建议将实际更改分发回主线程.您可以在后台线程中执行所需的任何计算,并使用performSelectorOnMainThread来实际更改标题.
| 归档时间: |
|
| 查看次数: |
847 次 |
| 最近记录: |