]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynload.cpp
fixed double deletion (patch #945491)
[wxWidgets.git] / src / common / dynload.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dynload.cpp
3 // Purpose: Dynamic loading framework
4 // Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's
5 // (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux)
6 // Modified by:
7 // Created: 03/12/01
8 // RCS-ID: $Id$
9 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "dynload.h"
15 #endif
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DYNAMIC_LOADER
28
29 #ifdef __WINDOWS__
30 #include "wx/msw/private.h"
31 #endif
32
33 #ifndef WX_PRECOMP
34 #include "wx/log.h"
35 #include "wx/intl.h"
36 #include "wx/hash.h"
37 #include "wx/utils.h"
38 #endif
39
40 #include "wx/strconv.h"
41
42 #include "wx/dynload.h"
43 #include "wx/module.h"
44
45
46 // ---------------------------------------------------------------------------
47 // wxPluginLibrary
48 // ---------------------------------------------------------------------------
49
50
51 wxDLImports* wxPluginLibrary::ms_classes = NULL;
52
53 class wxPluginLibraryModule : public wxModule
54 {
55 public:
56 wxPluginLibraryModule() { }
57
58 // TODO: create ms_classes on demand, why always preallocate it?
59 virtual bool OnInit()
60 {
61 wxPluginLibrary::ms_classes = new wxDLImports;
62 wxPluginManager::CreateManifest();
63 return TRUE;
64 }
65
66 virtual void OnExit()
67 {
68 delete wxPluginLibrary::ms_classes;
69 wxPluginLibrary::ms_classes = NULL;
70 wxPluginManager::ClearManifest();
71 }
72
73 private:
74 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule )
75 };
76
77 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule)
78
79
80 wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags)
81 : m_linkcount(1)
82 , m_objcount(0)
83 {
84 m_before = wxClassInfo::sm_first;
85 Load( libname, flags );
86 m_after = wxClassInfo::sm_first;
87
88 if( m_handle != 0 )
89 {
90 UpdateClasses();
91 RegisterModules();
92 }
93 else
94 {
95 // Flag us for deletion
96 --m_linkcount;
97 }
98 }
99
100 wxPluginLibrary::~wxPluginLibrary()
101 {
102 if( m_handle != 0 )
103 {
104 UnregisterModules();
105 RestoreClasses();
106 }
107 }
108
109 wxPluginLibrary *wxPluginLibrary::RefLib()
110 {
111 wxCHECK_MSG( m_linkcount > 0, NULL,
112 _T("Library had been already deleted!") );
113
114 ++m_linkcount;
115 return this;
116 }
117
118 bool wxPluginLibrary::UnrefLib()
119 {
120 wxASSERT_MSG( m_objcount == 0,
121 _T("Library unloaded before all objects were destroyed") );
122
123 if ( --m_linkcount == 0 )
124 {
125 delete this;
126 return TRUE;
127 }
128
129 return FALSE;
130 }
131
132 // ------------------------
133 // Private methods
134 // ------------------------
135
136 void wxPluginLibrary::UpdateClasses()
137 {
138 for (wxClassInfo *info = m_after; info != m_before; info = info->m_next)
139 {
140 if( info->m_className )
141 {
142 // Hash all the class names into a local table too so
143 // we can quickly find the entry they correspond to.
144 (*ms_classes)[info->m_className] = this;
145 }
146 }
147 }
148
149 void wxPluginLibrary::RestoreClasses()
150 {
151 for(wxClassInfo *info = m_after; info != m_before; info = info->m_next)
152 {
153 ms_classes->erase(ms_classes->find(info->m_className));
154 }
155 }
156
157 void wxPluginLibrary::RegisterModules()
158 {
159 // Plugin libraries might have wxModules, Register and initialise them if
160 // they do.
161 //
162 // Note that these classes are NOT included in the reference counting since
163 // it's implicit that they will be unloaded if and when the last handle to
164 // the library is. We do have to keep a copy of the module's pointer
165 // though, as there is currently no way to Unregister it without it.
166
167 wxASSERT_MSG( m_linkcount == 1,
168 _T("RegisterModules should only be called for the first load") );
169
170 for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next)
171 {
172 if( info->IsKindOf(CLASSINFO(wxModule)) )
173 {
174 wxModule *m = wxDynamicCast(info->CreateObject(), wxModule);
175
176 wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") );
177
178 m_wxmodules.push_back(m);
179 wxModule::RegisterModule(m);
180 }
181 }
182
183 // FIXME: Likewise this is (well was) very similar to InitializeModules()
184
185 for ( wxModuleList::iterator it = m_wxmodules.begin();
186 it != m_wxmodules.end();
187 ++it)
188 {
189 if( !(*it)->Init() )
190 {
191 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
192
193 // XXX: Watch this, a different hash implementation might break it,
194 // a good hash implementation would let us fix it though.
195
196 // The name of the game is to remove any uninitialised modules and
197 // let the dtor Exit the rest on shutdown, (which we'll initiate
198 // shortly).
199
200 wxModuleList::iterator oldNode = m_wxmodules.end();
201 do {
202 ++it;
203 if( oldNode != m_wxmodules.end() )
204 m_wxmodules.erase(oldNode);
205 wxModule::UnregisterModule( *it );
206 oldNode = it;
207 } while( it != m_wxmodules.end() );
208
209 --m_linkcount; // Flag us for deletion
210 break;
211 }
212 }
213 }
214
215 void wxPluginLibrary::UnregisterModules()
216 {
217 wxModuleList::iterator it;
218
219 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
220 (*it)->Exit();
221
222 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
223 wxModule::UnregisterModule( *it );
224
225 // NB: content of the list was deleted by UnregisterModule calls above:
226 m_wxmodules.clear();
227 }
228
229
230 // ---------------------------------------------------------------------------
231 // wxPluginManager
232 // ---------------------------------------------------------------------------
233
234 wxDLManifest* wxPluginManager::ms_manifest = NULL;
235
236 // ------------------------
237 // Static accessors
238 // ------------------------
239
240 wxPluginLibrary *
241 wxPluginManager::LoadLibrary(const wxString &libname, int flags)
242 {
243 wxString realname(libname);
244
245 if( !(flags & wxDL_VERBATIM) )
246 realname += wxDynamicLibrary::GetDllExt();
247
248 wxPluginLibrary *entry;
249
250 if ( flags & wxDL_NOSHARE )
251 {
252 entry = NULL;
253 }
254 else
255 {
256 entry = FindByName(realname);
257 }
258
259 if ( entry )
260 {
261 wxLogTrace(_T("dll"),
262 _T("LoadLibrary(%s): already loaded."), realname.c_str());
263
264 entry->RefLib();
265 }
266 else
267 {
268 entry = new wxPluginLibrary( libname, flags );
269
270 if ( entry->IsLoaded() )
271 {
272 (*ms_manifest)[realname] = entry;
273
274 wxLogTrace(_T("dll"),
275 _T("LoadLibrary(%s): loaded ok."), realname.c_str());
276
277 }
278 else
279 {
280 wxLogTrace(_T("dll"),
281 _T("LoadLibrary(%s): failed to load."), realname.c_str());
282
283 // we have created entry just above
284 if ( !entry->UnrefLib() )
285 {
286 // ... so UnrefLib() is supposed to delete it
287 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
288 }
289
290 entry = NULL;
291 }
292 }
293
294 return entry;
295 }
296
297 bool wxPluginManager::UnloadLibrary(const wxString& libname)
298 {
299 wxString realname = libname;
300
301 wxPluginLibrary *entry = FindByName(realname);
302
303 if ( !entry )
304 {
305 realname += wxDynamicLibrary::GetDllExt();
306
307 entry = FindByName(realname);
308 }
309
310 if ( !entry )
311 {
312 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
313 libname.c_str());
314
315 return FALSE;
316 }
317
318 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname.c_str());
319
320 if ( !entry->UnrefLib() )
321 {
322 // not really unloaded yet
323 return FALSE;
324 }
325
326 ms_manifest->erase(ms_manifest->find(realname));
327
328 return TRUE;
329 }
330
331 // ------------------------
332 // Class implementation
333 // ------------------------
334
335 bool wxPluginManager::Load(const wxString &libname, int flags)
336 {
337 m_entry = wxPluginManager::LoadLibrary(libname, flags);
338
339 return IsLoaded();
340 }
341
342 void wxPluginManager::Unload()
343 {
344 wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") );
345
346 for ( wxDLManifest::iterator i = ms_manifest->begin();
347 i != ms_manifest->end();
348 ++i )
349 {
350 if ( i->second == m_entry )
351 {
352 ms_manifest->erase(i);
353 break;
354 }
355 }
356
357 m_entry->UnrefLib();
358
359 m_entry = NULL;
360 }
361
362
363
364 #if WXWIN_COMPATIBILITY_2_2
365
366 wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle)
367 {
368 for ( wxDLManifest::iterator i = ms_manifest->begin();
369 i != ms_manifest->end();
370 ++i )
371 {
372 wxPluginLibrary * const lib = i->second;
373
374 if ( lib->GetLibHandle() == handle )
375 return lib;
376 }
377
378 return NULL;
379 }
380
381 // ---------------------------------------------------------------------------
382 // wxDllLoader (all these methods are static)
383 // ---------------------------------------------------------------------------
384
385
386 wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success)
387 {
388 wxPluginLibrary *p = wxPluginManager::LoadLibrary
389 (
390 name,
391 wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE
392 );
393
394 if ( success )
395 *success = p != NULL;
396
397 return p ? p->GetLibHandle() : 0;
398 }
399
400 void wxDllLoader::UnloadLibrary(wxDllType handle)
401 {
402 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle);
403
404 wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") );
405
406 p->UnrefLib();
407 }
408
409 void *
410 wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
411 {
412 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle);
413
414 if ( !p )
415 {
416 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
417
418 if ( success )
419 *success = FALSE;
420
421 return NULL;
422 }
423
424 return p->GetSymbol(name, success);
425 }
426
427
428 // ---------------------------------------------------------------------------
429 // Global variables
430 // ---------------------------------------------------------------------------
431
432 wxLibraries wxTheLibraries;
433
434 // ============================================================================
435 // implementation
436 // ============================================================================
437
438 // construct the full name from the base shared object name: adds a .dll
439 // suffix under Windows or .so under Unix
440 static wxString ConstructLibraryName(const wxString& basename)
441 {
442 wxString fullname;
443 fullname << basename << wxDllLoader::GetDllExt();
444
445 return fullname;
446 }
447
448 // ---------------------------------------------------------------------------
449 // wxLibrary (one instance per dynamic library)
450 // ---------------------------------------------------------------------------
451
452 wxLibrary::wxLibrary(wxDllType handle)
453 {
454 typedef wxClassInfo *(*t_get_first)(void);
455 t_get_first get_first;
456
457 m_handle = handle;
458
459 // Some system may use a local heap for library.
460 get_first = (t_get_first)GetSymbol(_T("wxGetClassFirst"));
461 // It is a wxWidgets DLL.
462 if (get_first)
463 PrepareClasses(get_first());
464 }
465
466 wxLibrary::~wxLibrary()
467 {
468 if ( m_handle )
469 {
470 wxDllLoader::UnloadLibrary(m_handle);
471 }
472 }
473
474 wxObject *wxLibrary::CreateObject(const wxString& name)
475 {
476 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
477
478 if (!info)
479 return NULL;
480
481 return info->CreateObject();
482 }
483
484 void wxLibrary::PrepareClasses(wxClassInfo *first)
485 {
486 // Index all class infos by their class name
487 wxClassInfo *info = first;
488 while (info)
489 {
490 if (info->m_className)
491 classTable.Put(info->m_className, (wxObject *)info);
492 info = info->m_next;
493 }
494
495 #if !wxUSE_EXTENDED_RTTI
496 // Set base pointers for each wxClassInfo
497 info = first;
498 while (info)
499 {
500 if (info->GetBaseClassName1())
501 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
502 if (info->GetBaseClassName2())
503 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
504 info = info->m_next;
505 }
506 #endif
507 }
508
509 void *wxLibrary::GetSymbol(const wxString& symbname)
510 {
511 return wxDllLoader::GetSymbol(m_handle, symbname);
512 }
513
514
515 // ---------------------------------------------------------------------------
516 // wxLibraries (only one instance should normally exist)
517 // ---------------------------------------------------------------------------
518
519 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
520 {
521 }
522
523 wxLibraries::~wxLibraries()
524 {
525 wxNode *node = m_loaded.First();
526
527 while (node) {
528 wxLibrary *lib = (wxLibrary *)node->Data();
529 delete lib;
530
531 node = node->Next();
532 }
533 }
534
535 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
536 {
537 wxLibrary *lib;
538 wxClassInfo *old_sm_first;
539 wxNode *node = m_loaded.Find(name.GetData());
540
541 if (node != NULL)
542 return ((wxLibrary *)node->Data());
543
544 // If DLL shares data, this is necessary.
545 old_sm_first = wxClassInfo::sm_first;
546 wxClassInfo::sm_first = NULL;
547
548 wxString libname = ConstructLibraryName(name);
549
550 bool success = FALSE;
551 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
552 if(success)
553 {
554 lib = new wxLibrary(handle);
555 wxClassInfo::sm_first = old_sm_first;
556
557 m_loaded.Append(name.GetData(), lib);
558 }
559 else
560 lib = NULL;
561 return lib;
562 }
563
564 wxObject *wxLibraries::CreateObject(const wxString& path)
565 {
566 wxNode *node = m_loaded.First();
567 wxObject *obj;
568
569 while (node) {
570 obj = ((wxLibrary *)node->Data())->CreateObject(path);
571 if (obj)
572 return obj;
573
574 node = node->Next();
575 }
576 return NULL;
577 }
578
579 #endif // WXWIN_COMPATIBILITY_2_2
580
581
582 #endif // wxUSE_DYNAMIC_LOADER
583