[Clam-devel] reloading plugins and static variables...

David García Garzón dgarcia at iua.upf.edu
Thu Aug 7 17:23:53 PDT 2008


Using an static member could lead to problems as there is no guarantee that 
one static variable is initialized before another one. Best use a static 
local variable in a function (often a static method in a class):

class X
{
	static string & currentLibrary()
	{
		static string sCurrentLibrary;
		return sCurrentLibrary;
	}
}

C++ guarantees that sCurrentLibrary is initialized the first time you enter 
the function, but then as the object is static (in the sense of not in stack) 
when you change the value it is kept for the next call. This way, if any 
other static initialization calls the function, the object is initialized 
before using it.

This trick is used in a lot of places in CLAM, mostly Factories and similar 
objects.

David.


On Divendres 08 Agost 2008, Natanael Olaiz wrote:
> Hi.
>
> I'm testing the David suggestions to keep a list of loaded dynamic
> libraries (i.e. plugins), and the currently loaded.
> I declared:
>
>     public:
>         static std::string _currentlyLoading;
>
> on RunTimeLibraryLoader.hxx. Is initialized on RunTimeLibraryLoader.cxx as:
>
>     std::string RunTimeLibraryLoader::_currentlyLoading=std::string();
>
> Is used it on virtual RunTimeLibraryLoader::FullyLoadLibrary, which is
> called from virtual RunTimeLibraryLoader::LoadLibrariesFromPath, which
> is called from virtual RunTimeLibraryLoader::Load.
> For ladspa plugins loadings (calling Load() from a derived class) it
> works OK. But on AutomaticRunTimeProcessingLibraryLoader (which is on
> RunTimeProcessingLibraryLoader.cxx, instantiates a derived class from
> RunTimeLibraryLoader as an object, and call the Load() from there) gaves
> me a segmentation fault, like if the static variable were not defined.
> Furthermore, trying to access directly
> RunTimeLibraryLoader::_currentlyLoading (for instance:
> RunTimeLibraryLoader::_currentlyLoading="testing";) is the same.
>
> TraceBack:
>     #0: std::string::asign       /usr/lib/libstdc++.so.6
>     #1: std::string::operator=   /usr/lib/libstdc++.so.6
>     #2: AutomaticRunTimeProcessingLibraryLoader   (the above assignation
> _currentlyLoading="testing")
>
> AutomaticRun..... is instantiated as a static object. Could be that the
> problem? I tried defining the variable as extern, without luck...
>
> This is making me mad... :-/ The linker doesn't says anything when
> compiling...
>
> Any clue?
>
>
> Regards,
> Natanael.



-- 
David García Garzón
(Work) dgarcia at iua dot upf anotherdot es
http://www.iua.upf.edu/~dgarcia
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.clam-project.org/pipermail/clam-devel-clam-project.org/attachments/20080808/0de09fc2/attachment-0003.pgp>


More information about the clam-devel mailing list