DWM Leaks Memory When No Monitor Connected Windows 8 - windows

Simply, if no monitor connected and you are doing some "windows" operation on windows 8 embedded, dwm.exe starts allocating memory and never stops.
to regenerate and show you all the problem, i have written an application which is doing "Create a new Form and show it, if there is one created close it first"
public partial class PopupWindows : Form
{
private Timer _t;
private Form _form;
public PopupWindows()
{
InitializeComponent();
this.Size = new Size(500, 500);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_t = new Timer();
_t.Tick += (o, ea) =>
{
_form?.Close();
(_form = new Form() { Size = this.Size }).Show();
};
_t.Interval = 1000;
_t.Enabled = true;
}
}
if you run this application and plug out the monitor, dwm start allocating pages
here is the output from pslist -m when the monitor connected
Name Pid VM WS Priv Priv Pk Faults NonP Page
dwm 840 123528 14400 11264 13044 55125 7 212
after some time but still the monitor connected
dwm 840 117144 15808 12732 13044 94051 7 200
disconnecting monitor and dwm.exe start allocating
dwm 840 214660 14444 12664 13044 137409 13 400
below is the output where you can see allocation dwm.exe doing
C:\PSTools>pslist.exe -m|findstr "dwm"
dwm 840 251956 14460 12740 13044 137413 16 477
C:\PSTools>pslist.exe -m|findstr "dwm"
dwm 840 251956 14460 12740 13044 137413 16 477
C:\PSTools>pslist.exe -m|findstr "dwm"
dwm 840 251956 14460 12740 13044 137413 16 477
C:\PSTools>pslist.exe -m|findstr "dwm"
dwm 840 252964 14460 12740 13044 137413 16 479
C:\PSTools>pslist.exe -m|findstr "dwm"
dwm 840 252964 14460 12740 13044 137413 16 479
C:\PSTools>pslist.exe -m|findstr "dwm"
dwm 840 253972 14460 12744 13044 137413 16 481
C:\PSTools>pslist.exe -m|findstr "dwm"
dwm 840 253972 14460 12744 13044 137413 16 481
C:\PSTools>pslist.exe -m|findstr "dwm"
dwm 840 254980 14460 12744 13044 137413 16 483
and this strange act dwm doing never stops till you connect a monitor or doing vnc connection to the computer.
after some time on operation without monitor, every os component starts to complain about memory and a blue screen of death occurs at the end.
let me show you the last one
dwm 840 807516 15624 14216 17240 147413 50 1603
here is the output showing how many minutes my popupwindows.exe running
Name Pid Pri Thd Hnd Priv CPU Time Elapsed Time
PopupWindows 2052 6 4 138 9468 0:00:00.140 0:23:07.332
as you can see, after about 20 minutes without having a monitor on the device, dwm.exe using 807516 bytes VM and 1603 pages.

Acting like what vnc doing (copying whole screen bitmap to application memory regularly) solved the dwm's allocation problem (which i think dwm caches its outputs but no one come and ask for the changes so it keeps all of them in the memory and never cleans).
below is the application which starts hidden and regularly copying screen image into app memory
public partial class HiddenForm : Form
{
IntPtr _this;
private System.Windows.Forms.Timer _timer;
private Bitmap _bmp = null;
public HiddenForm()
{
this.Visible = false;
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.None;
this.HandleCreated += (s, e) => _this = this.Handle;
this.Load += (s, e) => this.Size = new Size(0, 0);
CreateHandle();
CreateControl();
_timer = new System.Windows.Forms.Timer();
_timer.Tick += _timer_Tick;
_timer.Interval = 2000;
_timer.Start();
}
private void _timer_Tick(object sender, EventArgs e)
{
if (Screen.PrimaryScreen == null)
return;
if (_bmp == null)
{
using (Graphics g = CreateGraphics())
{
_bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g);
}
}
using (Graphics grp = Graphics.FromImage(_bmp))
{
grp.CopyFromScreen(0, 0, 0, 0, _bmp.Size);
}
}
}
here is the output after starting my app
Name Pid VM WS Priv Priv Pk Faults NonP Page
dwm 832 117876 20188 19624 35004 184497 7 185
killing my app with pskill waiting a while
dwm 832 182388 20264 19852 35004 421343 11 317
it starts increasing to re check our app i started it again by typing
C:\HiddenForm.exe
and dwm.exe memory allocation magically downed.
dwm 832 126384 27900 27364 35144 556729 7 187

Related

NSDrawer causes crash when added to App Window

I'm trying to programmatically add an NSDrawer to my app's main window (which also has an NSOutlineViewwhich was setup in IB):
Ivar:
var dd: NSDrawer? = nil
Then, when a disclosure triangle button is clicked:
if dd == nil {
var drawer_rect = NSInsetRect(self.window.frame, 30, 30).size
drawer_rect.height = 150
dd = NSDrawer.init(contentSize: drawer_rect, preferredEdge: NSRectEdge.minY)
dd!.contentView = self.status_scroll
dd!.parentWindow = self.window
}
So when the user tries to 'disclose' the drawer, the above code creates an NSDrawer and then displays it. It all works fine but Xcode dumps the following out as the parentWindow is set:
[General] ERROR: Setting <NSOutlineView: 0x100f0b9b0> as the first responder for window <NSDrawerWindow: 0x100fc8900>, but it is in a different window (<NSWindow: 0x6080001e0600>)! This would eventually crash when the view is freed. The first responder will be set to nil.
(
0 AppKit 0x00007fff9fc289cf -[NSWindow _validateFirstResponder:] + 557
1 AppKit 0x00007fff9f3a374c -[NSWindow _setFirstResponder:] + 31
2 AppKit 0x00007fff9f90c35b -[NSDrawerWindow _setParentWindow:] + 64
3 AppKit 0x00007fff9f90b666 -[NSDrawer(DrawerInternals) _doSetParentWindow:] + 382
4 AppKit 0x00007fff9f907786 -[NSDrawer setParentWindow:] + 78
Comment out the setting of the parentWindow and nothing gets dumped to the console.
NSDrawer is deprecated as per Apple documentation. You should consider a different design. If you still use NSDrawer, you may face such issues.

How to update an NSTextView while user is scrolling (without crashing)

I'm using an NSTextView to display the result of a long search, where lines are added as they are found by a background thread using
[self performSelectorOnMainThread: #selector(addMatch:)
withObject:options waitUntilDone:TRUE];
As the update routine I have
-(void)addMatch:(NSDictionary*)options{
...
NSTextStorage* store = [textView textStorage];
[store beginEditing];
[store appendAttributedString:text];
...
[store endEditing];
}
This works fine, until the user scrolls through the matches as they are being updated, at which point there's an exception
-[NSLayoutManager _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] *** attempted layout while textStorage is editing. It is not valid to
cause the layoutManager to do layout while the textStorage is editing
(ie the textStorage has been sent a beginEditing message without a
matching endEditing.)
within a layout call:
0 CoreFoundation 0x00007fff92ea364c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8acd16de objc_exception_throw + 43
2 CoreFoundation 0x00007fff92ea34fd +[NSException raise:format:] + 205
3 UIFoundation 0x00007fff8fe4fbc1 -[NSLayoutManager(NSPrivate) _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] + 641
4 UIFoundation 0x00007fff8fe5970c _NSFastFillAllLayoutHolesForGlyphRange + 1493
5 UIFoundation 0x00007fff8fda8821 -[NSLayoutManager lineFragmentRectForGlyphAtIndex:effectiveRange:] + 39
6 AppKit 0x00007fff8ef3cb02 -[NSTextView _extendedGlyphRangeForRange:maxGlyphIndex:drawingToScreen:] + 478
7 AppKit 0x00007fff8ef3ba97 -[NSTextView drawRect:] + 1832
8 AppKit 0x00007fff8eed9a09 -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] + 1186
9 AppKit 0x00007fff8eed9458 __46-[NSView(NSLayerKitGlue) drawLayer:inContext:]_block_invoke + 218
10 AppKit 0x00007fff8eed91f1 -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 2407
11 AppKit 0x00007fff8eed8873 -[NSView(NSLayerKitGlue) drawLayer:inContext:] + 108
12 AppKit 0x00007fff8efaafd2 -[NSTextView drawLayer:inContext:] + 179
13 AppKit 0x00007fff8ef22f76 -[_NSBackingLayerContents drawLayer:inContext:] + 145
14 QuartzCore 0x00007fff9337c177 -[CALayer drawInContext:] + 119
15 AppKit 0x00007fff8ef22aae -[_NSTiledLayer drawTile:inContext:] + 625
16 AppKit 0x00007fff8ef227df -[_NSTiledLayerContents drawLayer:inContext:] + 169
17 QuartzCore 0x00007fff9337c177 -[CALayer drawInContext:] + 119
18 AppKit 0x00007fff8f6efd64 -[NSTileLayer drawInContext:] + 169
19 QuartzCore 0x00007fff9337b153 CABackingStoreUpdate_ + 3306
20 QuartzCore 0x00007fff9337a463 ___ZN2CA5Layer8display_Ev_block_invoke + 59
21 QuartzCore 0x00007fff9337a41f x_blame_allocations + 81
22 QuartzCore 0x00007fff93379f1c _ZN2CA5Layer8display_Ev + 1546
23 AppKit 0x00007fff8ef226ed -[NSTileLayer display] + 119
24 AppKit 0x00007fff8ef1ec34 -[_NSTiledLayerContents update:] + 5688
25 AppKit 0x00007fff8ef1d337 -[_NSTiledLayer display] + 375
26 QuartzCore 0x00007fff93379641 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 603
27 QuartzCore 0x00007fff93378d7d _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 35
28 QuartzCore 0x00007fff9337850e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
29 QuartzCore 0x00007fff93378164 _ZN2CA11Transaction6commitEv + 390
30 QuartzCore 0x00007fff93388f55 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 71
31 CoreFoundation 0x00007fff92dc0d87 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
32 CoreFoundation 0x00007fff92dc0ce0 __CFRunLoopDoObservers + 368
33 CoreFoundation 0x00007fff92db2f1a __CFRunLoopRun + 1178
34 CoreFoundation 0x00007fff92db2838 CFRunLoopRunSpecific + 296
35 UIFoundation 0x00007fff8fdfe744 -[NSHTMLReader _loadUsingWebKit] + 2097
36 UIFoundation 0x00007fff8fdffb55 -[NSHTMLReader attributedString] + 22
37 UIFoundation 0x00007fff8fe12cca _NSReadAttributedStringFromURLOrData + 10543
38 UIFoundation 0x00007fff8fe10306 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 115
What is wrong, given that everything is between beginEditing and endEditing?
From the stack trace (which is not complete), it looks like a run loop source is firing at an inopportune moment.
NSAttributedString uses WebKit to parse HTML. WebKit sometimes runs the run loop. For the general case, it may need to fetch resources from the network to render properly. Since that takes time, it runs the run loop to wait for the result and process other things at the same time.
One of the other run loop sources seems to be a Core Animation source to do the next step in some animation (scrolling the text view, presumably).
You didn't show all of the code between beginEditing and endEditing. I suspect you have constructed an NSAttributedString from HTML or data fetched from a URL in between those two places. That allows the Core Animation run loop source to fire. That asks the text view to draw, which asks its layout manager to lay out the text. This is occurring after beginEditing but before endEditing, which is the cause for the exception.
So, try reordering your code to construct all NSAttributedStrings before beginEditing.
And file a bug with Apple. In my opinion, when NSAttributeString uses WebKit to render HTML, it needs to make WebKit use a private run loop mode so no other sources can fire. They may prefer a different solution, but the bug is real.
As far as I can tell, there's no fix for this. An alternative that works is storing the matches as attributed strings in an array, and using an NSTableView to show the matches by setting the textField.attributedStringValue (calling reloadData every time you add a new match); something like this (where matchContent is an NSMutableArray):
-(void)addMatch:(NSDictionary*)options{
...
[matchContent addObject:text];
[resultTableView reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return matchContent.count;
}
- (NSView *)tableView:(NSTableView *)tableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row {
NSTableCellView *result = [tableView makeViewWithIdentifier:#"MyView" owner:self];
result.textField.attributedStringValue = [matchContent objectAtIndex:row];
return result;
}
If the result is multi-line, you may also need to check the autoresizing mask of the cell/text field, and return the row height for the table view using the attributed string's boundingRectWithSize method.

Firefox Extension/Addon - Detect workspace locked

I'm searching for a solution in my firefox addon to detect when the user workspace has locked/released. In google chrome there is an easy API chrome.idle.onStateChanged.addListener for it, is there something similar in firefox or any possibility to do that, platform independent?
By the way, I use the addon sdk.
I've already tried the idle service:
Cc["#mozilla.org/widget/idleservice;1"].getService(Ci.nsIIdleService)
but I just gives me access to some idle timeout or system when go to sleep and not to just workspace locked.
Edit: With "workspace locked" I mean the user lock the workspace with ctrl + alt + delete. I don't know how this exactly work on OSX or linux.
Edit2: I working in Windows 8.1 currently, but I guess the chrome.idle handler works cross platform.
Edit3: What i currently get out of current answers is
1. That there not exist a cross-platform solution, neither in chrome nor in firefox.
chrome.idle.onStateChanged seems to work different on windows, linux and osx. Only windows can handle that "locked" behaviour as expected. I can't test OSX, on ubuntu 14 it doesn't work for me.
2. For firefox there some in-depth code things to try to make it working - see answer bellow from Noitidart in this topic.
Edit4: Noitidart have found a solution for windows - github link.
I don't know how to detect screen lock but there are these observer notifications:
https://developer.mozilla.org/en-US/docs/Observer_Notifications#Idle_Service
Also the computer sleep wake notifications. ill ask around for lock screen thats an interesting one.
Some useful chat about the subject, looking at how google chrome does it:
[12:33] ok guys question about actual work. anyone know how to detect if screen was locked? apparently google chrome has a method: https://developer.chrome.com/extensions/idle#event-onStateChanged
[12:45] anyone know of a MXR or DXR for google chromes codebase?
[12:46] mxr.mozilla.org/chromium
[12:52] Ms2ger: can you help me find how they test screen lock. im looking here: http://mxr.mozilla.org/chromium/source/src/chrome/browser/extensions/api/idle/idle_api_unittest.cc#84
[12:56] oh yuck it looks like they poll: http://mxr.mozilla.org/chromium/source/src/chrome/browser/extensions/api/idle/idle_manager.h#118
maybe they arent polling.
check this out:
http://mxr.mozilla.org/chromium/source/src/chrome/browser/extensions/api/idle/idle_manager.cc#246
244 void IdleManager::UpdateIdleStateCallback(int idle_time) {
245 DCHECK(thread_checker_.CalledOnValidThread());
246 bool locked = idle_time_provider_->CheckIdleStateIsLocked();
247 int listener_count = 0;
leads to: http://mxr.mozilla.org/chromium/source/src/chrome/browser/idle_win.cc#52
52 bool CheckIdleStateIsLocked() {
53 return ui::IsWorkstationLocked() || IsScreensaverRunning();
54 }
so this leads us to test if screensaver running or workstation locked
leads to:
http://mxr.mozilla.org/chromium/search?string=IsWorkstationLocked
we see just one implementation (its curious because there is no linux support but it doesnt say so on the chrome docs page, so maybe i couldnt find it)
Windows
http://mxr.mozilla.org/chromium/source/src/ui/base/win/lock_state.cc#11
11 bool IsWorkstationLocked() {
12 bool is_locked = true;
13 HDESK input_desk = ::OpenInputDesktop(0, 0, GENERIC_READ);
14 if (input_desk) {
15 wchar_t name[256] = {0};
16 DWORD needed = 0;
17 if (::GetUserObjectInformation(
18 input_desk, UOI_NAME, name, sizeof(name), &needed)) {
19 is_locked = lstrcmpi(name, L"default") != 0;
20 }
21 ::CloseDesktop(input_desk);
22 }
23 return is_locked;
24 }
Mac
see the screensaver section below, the screenlock is handled via there as well
http://mxr.mozilla.org/chromium/search?string=IsScreensaverRunning&find=&findi=&filter=^%5B^\0%5D*%24&hitlimit=&tree=chromium
we see in this search results 2 implementations, mac and windows it looks like no support for linux, which is curious because the chrome.idle page doesnt mention this on docs, maybe i just couldnt find it
windows implementation: http://mxr.mozilla.org/chromium/source/src/chrome/browser/idle_win.cc#39
39 bool IsScreensaverRunning() {
40 DWORD result = 0;
41 if (::SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &result, 0))
42 return result != FALSE;
43 return false;
44 }
45
mac implementation: http://mxr.mozilla.org/chromium/source/src/chrome/browser/idle_mac.mm#28
28 - (id)init {
29 if ((self = [super init])) {
30 NSDistributedNotificationCenter* distCenter =
31 [NSDistributedNotificationCenter defaultCenter];
32 [distCenter addObserver:self
33 selector:#selector(onScreenSaverStarted:)
34 name:#"com.apple.screensaver.didstart"
35 object:nil];
36 [distCenter addObserver:self
37 selector:#selector(onScreenSaverStopped:)
38 name:#"com.apple.screensaver.didstop"
39 object:nil];
40 [distCenter addObserver:self
41 selector:#selector(onScreenLocked:)
42 name:#"com.apple.screenIsLocked"
43 object:nil];
44 [distCenter addObserver:self
45 selector:#selector(onScreenUnlocked:)
46 name:#"com.apple.screenIsUnlocked"
47 object:nil];
48 }
49 return self;
50 }
so to sum this all up:
[13:32] for windows its peice of cake winapi has call to test if screen is locked or scrensaver running
[13:32] for mac they dont have screen lock test. they just have screensaver test, but its an observer method
[13:32] for linux they dont have screen lock nor screen saver test. real odd. ill ask the guy if he knows which os's they support chrome.idle in
edit: actually i found the linux implementation. back from the search results of: CheckIdleStateLocked: http://mxr.mozilla.org/chromium/search?string=CheckIdleStateIsLocked
http://mxr.mozilla.org/chromium/source/src/chrome/browser/idle_linux.cc#24
24 bool CheckIdleStateIsLocked() {
25 // Usually the screensaver is used to lock the screen, so we do not need to
26 // check if the workstation is locked.
27 #if defined(OS_CHROMEOS)
28 return false;
29 #elif defined(USE_OZONE)
30 return false;
31 #else
32 return ScreensaverWindowFinder::ScreensaverWindowExists();
33 #endif
34 }
Leads to ask how is ScreensaverWindowExists we find this: http://mxr.mozilla.org/chromium/source/src/chrome/browser/screensaver_window_finder_x11.cc
15 bool ScreensaverWindowFinder::ScreensaverWindowExists() {
16 gfx::X11ErrorTracker err_tracker;
17 ScreensaverWindowFinder finder;
18 ui::EnumerateTopLevelWindows(&finder);
19 return finder.exists_ && !err_tracker.FoundNewError();
20 }
Leads to what is EnumerateTopLevelWindows http://mxr.mozilla.org/chromium/source/src/ui/base/x/x11_util.cc#1059:
1059 void EnumerateTopLevelWindows(ui::EnumerateWindowsDelegate* delegate) {
1060 std::vector<XID> stack;
1061 if (!ui::GetXWindowStack(ui::GetX11RootWindow(), &stack)) {
1062 // Window Manager doesn't support _NET_CLIENT_LIST_STACKING, so fall back
1063 // to old school enumeration of all X windows. Some WMs parent 'top-level'
1064 // windows in unnamed actual top-level windows (ion WM), so extend the
1065 // search depth to all children of top-level windows.
1066 const int kMaxSearchDepth = 1;
1067 ui::EnumerateAllWindows(delegate, kMaxSearchDepth);
1068 return;
1069 }
1070 XMenuList::GetInstance()->InsertMenuWindowXIDs(&stack);
1071
1072 std::vector<XID>::iterator iter;
1073 for (iter = stack.begin(); iter != stack.end(); iter++) {
1074 if (delegate->ShouldStopIterating(*iter))
1075 return;
1076 }
1077 }
1078
We say they call delegate->ShouldStopIterating which was seen in same file as ScreensaverWindowExists: http://mxr.mozilla.org/chromium/source/src/chrome/browser/screensaver_window_finder_x11.cc
22 bool ScreensaverWindowFinder::ShouldStopIterating(XID window) {
23 if (!ui::IsWindowVisible(window) || !IsScreensaverWindow(window))
24 return false;
25 exists_ = true;
26 return true;
27 }
Leads to ask what is IsWindowVisible and IsScreensaverWindow
*IsScreensaverWindow, in same file of ScreensaverWindowExists: http://mxr.mozilla.org/chromium/source/src/chrome/browser/screensaver_window_finder_x11.cc
29 bool ScreensaverWindowFinder::IsScreensaverWindow(XID window) const {
30 // It should occupy the full screen.
31 if (!ui::IsX11WindowFullScreen(window))
32 return false;
33
34 // For xscreensaver, the window should have _SCREENSAVER_VERSION property.
35 if (ui::PropertyExists(window, "_SCREENSAVER_VERSION"))
36 return true;
37
38 // For all others, like gnome-screensaver, the window's WM_CLASS property
39 // should contain "screensaver".
40 std::string value;
41 if (!ui::GetStringProperty(window, "WM_CLASS", &value))
42 return false;
43
44 return value.find("screensaver") != std::string::npos;
45 }
IsWindowVisible: http://mxr.mozilla.org/chromium/source/src/ui/base/x/x11_util.cc#546
546 bool IsWindowVisible(XID window) {
547 TRACE_EVENT0("ui", "IsWindowVisible");
548
549 XWindowAttributes win_attributes;
550 if (!XGetWindowAttributes(gfx::GetXDisplay(), window, &win_attributes))
551 return false;
552 if (win_attributes.map_state != IsViewable)
553 return false;
554
555 // Minimized windows are not visible.
556 std::vector<XAtom> wm_states;
557 if (GetAtomArrayProperty(window, "_NET_WM_STATE", &wm_states)) {
558 XAtom hidden_atom = GetAtom("_NET_WM_STATE_HIDDEN");
559 if (std::find(wm_states.begin(), wm_states.end(), hidden_atom) !=
560 wm_states.end()) {
561 return false;
562 }
563 }

How to tell if an application is in modal state

I want to be able to tell if my application is currently in a "modal" state.
I know that if it is in this state using Cocoa's functions, I could tell by checking where [[NSApplication sharedApplication] modalWindow] returns nil or not.
But it could also be in modal state using Carbon's functions (RunAppModalLoopForWindow etc), and then Cocoa's modalWindow does not tell us whether the application is modal.
Unfortunately, I don't have the choice to avoid Carbon as my app hosts old third party plugins which do use it.
Here's part of an example stack trace in a modal state due to carbon:
frame #12: 0x93ede739 CoreFoundation`__CFRunLoopRun + 1897
frame #13: 0x93eddd5a CoreFoundation`CFRunLoopRunSpecific + 394
frame #14: 0x93eddbbb CoreFoundation`CFRunLoopRunInMode + 123
frame #15: 0x930cee2d HIToolbox`RunCurrentEventLoopInMode + 259
frame #16: 0x930cebb2 HIToolbox`ReceiveNextEventCommon + 526
frame #17: 0x93119c4a HIToolbox`AcquireNextEventInMode + 75
frame #18: 0x93269aea HIToolbox`_AcquireNextEvent + 58
frame #19: 0x932585dc HIToolbox`_RunAppModalLoop + 168
frame #20: 0x932584ee HIToolbox`RunAppModalLoopForWindow + 130
I could trace the stack and see that _RunAppModalLoop is there, but I don't like this solution.
You can try checking the output from GetWindowModality([[NSApp keyWindow] windowRef], ...) and/or the same applied to the -mainWindow.
You can see if -[NSRunLoop currentMode] is NSDefaultRunLoopMode.
However, you might get a different answer with a question more specific to the problem you're trying to solve.
This can be done by enumerating all the app's windows and checking if they are modal by using GetWindowModality.
bool isAnyCarbonWindowModal()
{
for (
WindowRef win = GetFrontWindowOfClass(kAllWindowClasses, true);
win != nullptr;
win = GetNextWindowOfClass(win, kAllWindowClasses, true))
{
WindowModality modalKind;
WindowRef unavailableWindow;
GetWindowModality(win, &modalKind, &unavailableWindow);
if (kWindowModalityAppModal == modalKind)
return true;
}
return false;
}

NSWorkspace setIcon:forFile:options: crashes

I am facing crash sometimes on [NSWorkspace setIcon:forFile:options:] API.
any help is appreciated.
here is crash stack,
0 libsystem_kernel.dylib 0x00007fff85bfbe56 __semwait_signal_nocancel + 10
1 libsystem_c.dylib 0x00007fff8c04818b nanosleep$NOCANCEL + 139
2 libsystem_c.dylib 0x00007fff8bfe7c78 usleep$NOCANCEL + 53
3 libsystem_c.dylib 0x00007fff8bfe7aa6 abort + 187
4 libsystem_c.dylib 0x00007fff8c04684c free + 389
5 com.apple.CoreServices.CarbonCore 0x00007fff8df7aaf7 CSMemDisposePtr + 23
6 com.apple.CoreServices.CarbonCore 0x00007fff8df7aaba CSMemDisposeHandle + 79
7 com.apple.CoreServices.CarbonCore 0x00007fff8df7c355 DisposeHandle + 9
8 com.apple.AppKit 0x00007fff90356598 -[NSWorkspace setIcon:forFile:options:] + 566
Here is code,
static NSImage *FolderIcon = nil;
if(!FolderIcon) {
FolderIcon = [[NSImage imageNamed:#"xxx.icns"] retain];
}
if(![[NSWorkspace sharedWorkspace] setIcon: FolderIcon
forFile:Path
options:0]) {
NSLog(#"error");
}
I am using static icon, so I don't think possibility of dangling pointer, Also I checked if we pass file path which does not exists then it return "NO". But it is not crashing.
This is rarely re-producible.. will paste errors once it re-produce
You should not call -setIcon:forFile:options: from multiple thread at a time.
From Documentation:
It is safe to call this method from any of your app’s threads, but you
must call it from only one thread at a time.

Resources