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 
41 static bool bInGDALGlobalDestructor = false;
42 extern "C" int CPL_DLL GDALIsInGlobalDestructor();
43 
GDALIsInGlobalDestructor()44 int GDALIsInGlobalDestructor()
45 {
46     return bInGDALGlobalDestructor;
47 }
48 
49 void CPLFinalizeTLS();
50 
51 /************************************************************************/
52 /*                           GDALDestroy()                              */
53 /************************************************************************/
54 
55 /** Finalize GDAL/OGR library.
56  *
57  * This function calls GDALDestroyDriverManager() and OGRCleanupAll() and
58  * finalize Thread Local Storage variables.
59  *
60  * Prior to GDAL 2.4.0, this function should normally be explicitly called by
61  * application code if GDAL is dynamically linked (but that does not hurt),
62  * since it was automatically called through
63  * the unregistration mechanisms of dynamic library loading.
64  *
65  * Since GDAL 2.4.0, this function may be called by application code, since
66  * it is no longer called automatically, on non-MSVC builds, due to ordering
67  * problems with respect to automatic destruction of global C++ objects.
68  *
69  * Note: no GDAL/OGR code should be called after this call!
70  *
71  * @since GDAL 2.0
72  */
73 
74 static bool bGDALDestroyAlreadyCalled = FALSE;
GDALDestroy(void)75 void GDALDestroy(void)
76 {
77     if( bGDALDestroyAlreadyCalled )
78         return;
79     bGDALDestroyAlreadyCalled = true;
80 
81     bInGDALGlobalDestructor = true;
82 
83     // logging/error handling may call GDALIsInGlobalDestructor()
84     CPLDebug("GDAL", "In GDALDestroy - unloading GDAL shared library.");
85 
86     GDALDestroyDriverManager();
87 
88     OGRCleanupAll();
89     GDALPythonFinalize();
90     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     CPLFreeConfig();
97     CPLFinalizeTLS();
98     CPLCleanupErrorMutex();
99     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 
GDALInitialize()116 static void GDALInitialize()
117 {
118     // nothing to do
119     //CPLDebug("GDAL", "Library loaded");
120 #ifdef DEBUG
121     const char* pszLocale = CPLGetConfigOption("GDAL_LOCALE", nullptr);
122     if( pszLocale )
123         CPLsetlocale( LC_ALL, pszLocale );
124 #endif
125 }
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 
DllMain(HINSTANCE,DWORD dwReason,LPVOID)138 extern "C" int WINAPI DllMain( HINSTANCE /* hInstance */,
139                                DWORD dwReason,
140                                LPVOID /* lpReserved */ )
141 {
142     if (dwReason == DLL_PROCESS_ATTACH)
143     {
144         // nothing to do
145     }
146     else if (dwReason == DLL_THREAD_ATTACH)
147     {
148         // nothing to do
149     }
150     else if (dwReason == DLL_THREAD_DETACH)
151     {
152         ::CPLCleanupTLS();
153     }
154     else if (dwReason == DLL_PROCESS_DETACH)
155     {
156         GDALDestroy();
157     }
158 
159     return 1; // ignored for all reasons but DLL_PROCESS_ATTACH
160 }
161 
162 #endif // CPL_DISABLE_DLL
163 #endif // _MSC_VER
164