Browse Source

DisplayManagerOSX: potential robustness improvements

Clear m_osxDisplayModes on reinit. This gets rid of stale state, and
also fixes a minor memory leak. (This is probably not all that critical,
because if everything else is sane, the entries that are going to be
accessed would have been overwritten anyway.)

Check and log CGGetActiveDisplayList() and CGDisplaySetDisplayMode()
return values.
Vincent Lang 8 năm trước cách đây
mục cha
commit
af0584fe10
1 tập tin đã thay đổi với 24 bổ sung3 xóa
  1. 24 3
      src/display/osx/DisplayManagerOSX.cpp

+ 24 - 3
src/display/osx/DisplayManagerOSX.cpp

@@ -10,14 +10,30 @@
 #include "utils/osx/OSXUtils.h"
 #include "DisplayManagerOSX.h"
 
+#include "QsLog.h"
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 bool DisplayManagerOSX::initialize()
 {
   int totalModes = 0;
-  
-  CGGetActiveDisplayList(MAX_DISPLAYS, m_osxDisplays, &m_osxnumDisplays);
+
   displays.clear();
 
+  for (int i = 0; i < m_osxDisplayModes.size(); i++)
+  {
+    if (m_osxDisplayModes[i])
+      CFRelease(m_osxDisplayModes[i]);
+  }
+  m_osxDisplayModes.clear();
+
+  CGError err = CGGetActiveDisplayList(MAX_DISPLAYS, m_osxDisplays, &m_osxnumDisplays);
+  if (err)
+  {
+    m_osxnumDisplays = 0;
+    QLOG_ERROR() << "CGGetActiveDisplayList returned failure:" << err;
+    return false;
+  }
+
   for (int displayid = 0; displayid < m_osxnumDisplays; displayid++)
   {
     // add the display to the list
@@ -83,7 +99,12 @@ bool DisplayManagerOSX::setDisplayMode(int display, int mode)
   CGDisplayModeRef displayMode =
   (CGDisplayModeRef)CFArrayGetValueAtIndex(m_osxDisplayModes[display], mode);
 
-  CGDisplaySetDisplayMode(m_osxDisplays[display], displayMode, NULL);
+  CGError err = CGDisplaySetDisplayMode(m_osxDisplays[display], displayMode, NULL);
+  if (err)
+  {
+    QLOG_ERROR() << "CGDisplaySetDisplayMode() returned failure:" << err;
+    return false;
+  }
 
   // HACK : on OSX, switching display mode can leave dock in a state where mouse cursor
   // will not hide on top of hidden dock, so we reset it state to fix this