Index: /OpenSceneGraph/trunk/src/osgViewer/DarwinUtils.h
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/DarwinUtils.h (revision 10208)
+++ /OpenSceneGraph/trunk/src/osgViewer/DarwinUtils.h (revision 11207)
@@ -91,5 +91,7 @@
     
     protected:
-    
+
+        virtual void _init();
+
         /** implementation of setScreenResolution */
         bool setScreenResolutionImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height) ;
@@ -118,4 +120,6 @@
     
     private:
+
+        bool                  _initialized;
         CGDisplayCount        _displayCount;
         CGDirectDisplayID*    _displayIds;
Index: /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCarbon.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCarbon.cpp (revision 11046)
+++ /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCarbon.cpp (revision 11207)
@@ -1054,7 +1054,21 @@
 class CarbonWindowingSystemInterface : public  DarwinWindowingSystemInterface {
 public:
-    CarbonWindowingSystemInterface()
-    :    DarwinWindowingSystemInterface()
-    {
+    CarbonWindowingSystemInterface() : DarwinWindowingSystemInterface()
+    {
+    }
+
+    virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits) 
+    {
+        _init();
+
+        return createGraphicsContextImplementation<PixelBufferCarbon, GraphicsWindowCarbon>(traits);
+    }
+
+    virtual void _init()
+    {
+        if (_initialized) return;
+
+        DarwinWindowingSystemInterface::init();
+
         // register application event handler and AppleEventHandler to get quit-events:
         static const EventTypeSpec menueventSpec = {kEventClassCommand, kEventCommandProcess};
@@ -1062,9 +1076,5 @@
         status = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(QuitAppleEventHandler), 0, false);
     }
-    
-    virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits) 
-    {
-        return createGraphicsContextImplementation<PixelBufferCarbon, GraphicsWindowCarbon>(traits);
-    }
+
 };
 
Index: /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCocoa.mm
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCocoa.mm (revision 10887)
+++ /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCocoa.mm (revision 11207)
@@ -1383,14 +1383,15 @@
 // ----------------------------------------------------------------------------------------------------------
 
-struct CocoaWindowingSystemInterface : public DarwinWindowingSystemInterface {
-    
-    CocoaWindowingSystemInterface()
-    :    DarwinWindowingSystemInterface()
-    
-    {        
-    }
-    
+struct CocoaWindowingSystemInterface : public DarwinWindowingSystemInterface
+{
+
+    CocoaWindowingSystemInterface() : DarwinWindowingSystemInterface()
+    {
+    }
+
     void initAsStandaloneApplication() 
     {
+        _init();
+
         static bool s_inited = false;
         if (s_inited) return;
@@ -1422,4 +1423,6 @@
     virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits) 
     {
+        _init();
+
         if (!traits->pbuffer) 
         {
Index: /OpenSceneGraph/trunk/src/osgViewer/DarwinUtils.mm
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/DarwinUtils.mm (revision 10622)
+++ /OpenSceneGraph/trunk/src/osgViewer/DarwinUtils.mm (revision 11207)
@@ -222,39 +222,68 @@
 /** ctor, get a list of all attached displays */
 DarwinWindowingSystemInterface::DarwinWindowingSystemInterface() :
+    _initialized(false),
     _displayCount(0),
     _displayIds(NULL)
 {
+}
+
+/** dtor */
+DarwinWindowingSystemInterface::~DarwinWindowingSystemInterface()
+{
+    if (osg::Referenced::getDeleteHandler())
+    {
+        osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
+        osg::Referenced::getDeleteHandler()->flushAll();
+    }
+
+    if (_displayIds) delete[] _displayIds;
+    _displayIds = NULL;
+}
+
+void DarwinWindowingSystemInterface::_init()
+{
+    if (_initialized) return;
+
     ProcessSerialNumber sn = { 0, kCurrentProcess };
     TransformProcessType(&sn,kProcessTransformToForegroundApplication);
     SetFrontProcess(&sn);
-    
+
     if( CGGetActiveDisplayList( 0, NULL, &_displayCount ) != CGDisplayNoErr )
+    {
         osg::notify(osg::WARN) << "DarwinWindowingSystemInterface: could not get # of screens" << std::endl;
-        
+        _displayCount = 0;
+
+        _initialized = true;
+        return;
+    }
+
     _displayIds = new CGDirectDisplayID[_displayCount];
+
     if( CGGetActiveDisplayList( _displayCount, _displayIds, &_displayCount ) != CGDisplayNoErr )
+    {
         osg::notify(osg::WARN) << "DarwinWindowingSystemInterface: CGGetActiveDisplayList failed" << std::endl;
-    
-    }
-
-/** dtor */
-DarwinWindowingSystemInterface::~DarwinWindowingSystemInterface()
-{
-    if (osg::Referenced::getDeleteHandler())
-    {
-        osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
-        osg::Referenced::getDeleteHandler()->flushAll();
-    }
-
-    if (_displayIds) delete[] _displayIds;
-    _displayIds = NULL;
+    }
+
+    _initialized = true;
 }
 
 /** @return a CGDirectDisplayID for a ScreenIdentifier */
-CGDirectDisplayID DarwinWindowingSystemInterface::getDisplayID(const osg::GraphicsContext::ScreenIdentifier& si) {
+CGDirectDisplayID DarwinWindowingSystemInterface::getDisplayID(const osg::GraphicsContext::ScreenIdentifier& si)
+{
+    _init();
+
+    if (_displayCount==0)
+    {
+        osg::notify(osg::WARN) << "DarwinWindowingSystemInterface::getDisplayID(..) no valid screens available returning 0 instead." << std::endl;
+        return 0;
+    }
+
     if (si.screenNum < static_cast<int>(_displayCount))
+    {
         return _displayIds[si.screenNum];
-    else {
-        osg::notify(osg::WARN) << "GraphicsWindowCarbon :: invalid screen # " << si.screenNum << ", returning main-screen instead" << std::endl;
+    }
+    else
+    {
+        osg::notify(osg::WARN) << "DarwinWindowingSystemInterface::getDisplayID(..) invalid screen # " << si.screenNum << ", returning main-screen instead." << std::endl;
         return _displayIds[0];
     }
@@ -264,4 +293,6 @@
 unsigned int DarwinWindowingSystemInterface::getNumScreens(const osg::GraphicsContext::ScreenIdentifier& si) 
 {
+    _init();
+
     return _displayCount;
 }
@@ -269,4 +300,15 @@
 void DarwinWindowingSystemInterface::getScreenSettings(const osg::GraphicsContext::ScreenIdentifier& si, osg::GraphicsContext::ScreenSettings & resolution)
 {
+    _init();
+
+    if (_displayCount==0)
+    {
+        resolution.width = 0;
+        resolution.height = 0;
+        resolution.colorDepth = 0;
+        resolution.refreshRate = 0;
+        return;
+    }
+
     CGDirectDisplayID id = getDisplayID(si);
     resolution.width = CGDisplayPixelsWide(id);
@@ -278,31 +320,49 @@
 
 
-void DarwinWindowingSystemInterface::enumerateScreenSettings(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, osg::GraphicsContext::ScreenSettingsList & resolutionList) {
-        // Warning! This method has not been tested.
-        resolutionList.clear();
-
-        CGDirectDisplayID displayid = getDisplayID(screenIdentifier);
-        CFArrayRef availableModes = CGDisplayAvailableModes(displayid);
-        unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes);
-        for (unsigned int i=0; i<numberOfAvailableModes; ++i) {
-            // look at each mode in the available list
-            CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i);
-            osg::GraphicsContext::ScreenSettings tmpSR;
-
-            long width = getDictLong(mode, kCGDisplayWidth);
-            tmpSR.width = width<=0 ? 0 : width;
-            long height = getDictLong(mode, kCGDisplayHeight);
-            tmpSR.height = height<=0 ? 0 : height;
-            long rate = getDictLong(mode, kCGDisplayRefreshRate);
-            tmpSR.refreshRate = rate<=0 ? 0 : rate;
-            long depth = getDictLong(mode, kCGDisplayBitsPerPixel);
-            tmpSR.colorDepth = depth<=0 ? 0 : depth;
-
-            resolutionList.push_back(tmpSR);
-        }
-    }
+void DarwinWindowingSystemInterface::enumerateScreenSettings(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, osg::GraphicsContext::ScreenSettingsList & resolutionList)
+{
+    _init();
+
+    // Warning! This method has not been tested.
+    resolutionList.clear();
+
+    if (_displayCount==0)
+    {
+        return;
+    }
+
+    CGDirectDisplayID displayid = getDisplayID(screenIdentifier);
+    CFArrayRef availableModes = CGDisplayAvailableModes(displayid);
+    unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes);
+    for (unsigned int i=0; i<numberOfAvailableModes; ++i) {
+        // look at each mode in the available list
+        CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i);
+        osg::GraphicsContext::ScreenSettings tmpSR;
+
+        long width = getDictLong(mode, kCGDisplayWidth);
+        tmpSR.width = width<=0 ? 0 : width;
+        long height = getDictLong(mode, kCGDisplayHeight);
+        tmpSR.height = height<=0 ? 0 : height;
+        long rate = getDictLong(mode, kCGDisplayRefreshRate);
+        tmpSR.refreshRate = rate<=0 ? 0 : rate;
+        long depth = getDictLong(mode, kCGDisplayBitsPerPixel);
+        tmpSR.colorDepth = depth<=0 ? 0 : depth;
+
+        resolutionList.push_back(tmpSR);
+    }
+}
 
 /** return the top left coord of a specific screen in global screen space */
-void DarwinWindowingSystemInterface::getScreenTopLeft(const osg::GraphicsContext::ScreenIdentifier& si, int& x, int& y) {
+void DarwinWindowingSystemInterface::getScreenTopLeft(const osg::GraphicsContext::ScreenIdentifier& si, int& x, int& y)
+{
+    _init();
+
+    if (_displayCount==0)
+    {
+        x = 0;
+        y = 0;
+        return;
+    }
+
     CGRect bounds = CGDisplayBounds( getDisplayID(si) );
     x = static_cast<int>(bounds.origin.x);
@@ -317,6 +377,8 @@
     bool result = setScreenResolutionImpl(si, settings.width, settings.height);
     if (result)
+    {
         setScreenRefreshRateImpl(si, settings.refreshRate);
-    
+    }
+
     return result;
 }
@@ -327,4 +389,11 @@
 bool DarwinWindowingSystemInterface::setScreenResolutionImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, unsigned int width, unsigned int height) 
 { 
+    _init();
+
+    if (_displayCount==0)
+    {
+        return false;
+    }
+
     CGDirectDisplayID displayid = getDisplayID(screenIdentifier);
     
@@ -345,6 +414,13 @@
 
 /** implementation of setScreenRefreshRate */
-bool DarwinWindowingSystemInterface::setScreenRefreshRateImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, double refreshRate) { 
-    
+bool DarwinWindowingSystemInterface::setScreenRefreshRateImpl(const osg::GraphicsContext::ScreenIdentifier& screenIdentifier, double refreshRate)
+{ 
+    _init();
+
+    if (_displayCount==0)
+    {
+        return false;
+    }
+
     boolean_t  success(false);
     unsigned width, height;
@@ -372,4 +448,11 @@
 unsigned int DarwinWindowingSystemInterface::getScreenContaining(int x, int y, int w, int h)
 {
+    _init();
+
+    if (_displayCount==0)
+    {
+        return 0;
+    }
+
     CGRect rect = CGRectMake(x,y,w,h);
     for(unsigned int i = 0; i < _displayCount; ++i) {
@@ -383,8 +466,3 @@
 }
 
-
-
-
-
-
-}
+}
