Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL Core 4 : * Purpose: The library set-up/clean-up routines. 5 : * Author: Mateusz Loskot <mateusz@loskot.net> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2010, Mateusz Loskot <mateusz@loskot.net> 9 : * Copyright (c) 2013, Even Rouault <even dot rouault at spatialys.com> 10 : * 11 : * Permission is hereby granted, free of charge, to any person obtaining a 12 : * copy of this software and associated documentation files (the "Software"), 13 : * to deal in the Software without restriction, including without limitation 14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 15 : * and/or sell copies of the Software, and to permit persons to whom the 16 : * Software is furnished to do so, subject to the following conditions: 17 : * 18 : * The above copyright notice and this permission notice shall be included 19 : * in all copies or substantial portions of the Software. 20 : * 21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 : * DEALINGS IN THE SOFTWARE. 28 : ****************************************************************************/ 29 : 30 : #include "cpl_port.h" 31 : #include "gdal.h" 32 : #include "gdalpython.h" 33 : 34 : #include "cpl_conv.h" 35 : #include "cpl_error.h" 36 : #include "cpl_multiproc.h" 37 : #include "cpl_string.h" 38 : #include "ogr_api.h" 39 : 40 : static bool bInGDALGlobalDestructor = false; 41 : extern "C" int CPL_DLL GDALIsInGlobalDestructor(); 42 : 43 289 : int GDALIsInGlobalDestructor() 44 : { 45 289 : return bInGDALGlobalDestructor; 46 : } 47 : 48 : void CPLFinalizeTLS(); 49 : 50 : static bool bGDALDestroyAlreadyCalled = FALSE; 51 : 52 : /************************************************************************/ 53 : /* GDALDestroy() */ 54 : /************************************************************************/ 55 : 56 : /** Finalize GDAL/OGR library. 57 : * 58 : * This function calls GDALDestroyDriverManager() and OGRCleanupAll() and 59 : * finalize Thread Local Storage variables. 60 : * 61 : * Prior to GDAL 2.4.0, this function should normally be explicitly called by 62 : * application code if GDAL is dynamically linked (but that does not hurt), 63 : * since it was automatically called through 64 : * the unregistration mechanisms of dynamic library loading. 65 : * 66 : * Since GDAL 2.4.0, this function may be called by application code, since 67 : * it is no longer called automatically, on non-MSVC builds, due to ordering 68 : * problems with respect to automatic destruction of global C++ objects. 69 : * 70 : * Note: no GDAL/OGR code should be called after this call! 71 : * 72 : * @since GDAL 2.0 73 : */ 74 : 75 387 : void GDALDestroy(void) 76 : { 77 387 : if (bGDALDestroyAlreadyCalled) 78 0 : return; 79 387 : bGDALDestroyAlreadyCalled = true; 80 : 81 387 : bInGDALGlobalDestructor = true; 82 : 83 : // logging/error handling may call GDALIsInGlobalDestructor() 84 387 : CPLDebug("GDAL", "In GDALDestroy - unloading GDAL shared library."); 85 : 86 387 : GDALDestroyDriverManager(); 87 : 88 387 : OGRCleanupAll(); 89 387 : GDALPythonFinalize(); 90 387 : bInGDALGlobalDestructor = false; 91 : 92 : /* See corresponding bug reports: */ 93 : /* https://trac.osgeo.org/gdal/ticket/6139 */ 94 : /* https://trac.osgeo.org/gdal/ticket/6868 */ 95 : /* Needed in case no driver manager has been instantiated. */ 96 387 : CPLFreeConfig(); 97 387 : CPLFinalizeTLS(); 98 387 : CPLCleanupErrorMutex(); 99 387 : CPLCleanupMasterMutex(); 100 : } 101 : 102 : /************************************************************************/ 103 : /* The library set-up/clean-up routines implemented with */ 104 : /* GNU C/C++ extensions. */ 105 : /* TODO: Is it Linux-only solution or Unix portable? */ 106 : /************************************************************************/ 107 : #ifdef __GNUC__ 108 : 109 : static void GDALInitialize() __attribute__((constructor)); 110 : 111 : /************************************************************************/ 112 : /* Called when GDAL is loaded by loader or by dlopen(), */ 113 : /* and before dlopen() returns. */ 114 : /************************************************************************/ 115 : 116 1242 : static void GDALInitialize() 117 : { 118 : // nothing to do 119 : // CPLDebug("GDAL", "Library loaded"); 120 : #ifdef DEBUG 121 1242 : const char *pszLocale = CPLGetConfigOption("GDAL_LOCALE", nullptr); 122 1242 : if (pszLocale) 123 0 : CPLsetlocale(LC_ALL, pszLocale); 124 : #endif 125 1242 : } 126 : 127 : #endif // __GNUC__ 128 : 129 : /************************************************************************/ 130 : /* The library set-up/clean-up routine implemented as DllMain entry */ 131 : /* point specific for Windows. */ 132 : /************************************************************************/ 133 : #ifdef _MSC_VER 134 : #ifndef CPL_DISABLE_DLL 135 : 136 : #include <windows.h> 137 : 138 : extern "C" int WINAPI DllMain(HINSTANCE /* hInstance */, DWORD dwReason, 139 : LPVOID /* lpReserved */) 140 : { 141 : if (dwReason == DLL_PROCESS_ATTACH) 142 : { 143 : // nothing to do 144 : } 145 : else if (dwReason == DLL_THREAD_ATTACH) 146 : { 147 : // nothing to do 148 : } 149 : else if (dwReason == DLL_THREAD_DETACH) 150 : { 151 : ::CPLCleanupTLS(); 152 : } 153 : else if (dwReason == DLL_PROCESS_DETACH) 154 : { 155 : GDALDestroy(); 156 : } 157 : 158 : return 1; // ignored for all reasons but DLL_PROCESS_ATTACH 159 : } 160 : 161 : #endif // CPL_DISABLE_DLL 162 : #endif // _MSC_VER