From b0db2a764f3b27b9a030b48b328db161b2218496 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Mon, 30 Dec 2013 15:23:01 +0000
Subject: [PATCH] Fix crash on exit (cancel the osgDB pager)

- borrow some code from the new reset codepath to cleanly stop the
OSG pager thread during normal shutdown. Avoids a race-condition
destroying various structures the pager thread might be modifying.
---
 src/Main/globals.cxx | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx
index c083f326f..9c714f6cb 100644
--- a/src/Main/globals.cxx
+++ b/src/Main/globals.cxx
@@ -27,6 +27,9 @@
 #include <boost/foreach.hpp>
 #include <algorithm>
 
+#include <osgViewer/Viewer>
+#include <osgDB/Registry>
+
 #include <simgear/structure/commands.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/misc/sg_dir.hxx>
@@ -209,6 +212,12 @@ FGGlobals::~FGGlobals()
     subsystem_mgr->remove("model-manager");
     _tile_mgr.clear();
 
+    // don't cancel the pager until after shutdown, since AIModels (and
+    // potentially others) can queue delete requests on the pager.
+    renderer->getViewer()->getDatabasePager()->cancel();
+    renderer->getViewer()->getDatabasePager()->clear();
+    osgDB::Registry::instance()->clearObjectCache();
+    
     // renderer touches subsystems during its destruction
     set_renderer(NULL);
     _scenery.clear();