[xsd-users] Re: MSVC - static variable initialisation

Martin Nickolas marty at tibra.com.au
Mon Oct 9 22:06:15 EDT 2006


Well, reading back over it, yesterday's post was much more complicated 
that it needed to be.

Here is the problem explained more simply:

XSD relies on the initialisation of a non-local object in each generated 
file to register that type with the parser.
In C++ non-local objects are only guaranteed to be initialised before 
any function in that translation unit is called.
Because I was using polymorphic types I never actually referred to my 
polymorphic types or functions on those types directly -- only via a 
pointer or a reference to the base type. So because I was never calling 
any functions in the translation units containing my polymorphic types 
the non-local object was never initialised and hence the parser failed 
to parse my valid xml file.
GCC always seems to init all non-local objects in all translation units 
in either the exe or a lib. However MSVC seems to init all non-local 
objects in the exe, but only non-local objects in translation units in 
libs that also contain functions that are called directly. I suspect 
this *may* be legal C++.

It was able to work around this problem simple by instantiating an 
instance of the polymorphic type declared in the generated file 
somewhere in the exe. Something like this:
int main()
{
    {
       MyPolymorphicXSDType dummy;
    }

    // run the program...
    return 0;
}
That's enough to cause the non-local object in the generated file to be 
initialised whether that translation unit is in the exe or the lib and 
built with GCC or MSVC.

CYA

Martin Nickolas wrote:
> Hi,
>
> I am using XSD 2.3.0 with GCC 3.4.6 and MSVC 8 and I am experiencing 
> some differences in behaviour between the two. I think the problem is 
> really a difference between the two compilers but perhaps someone can 
> shed some light on things...
>
> I have a base schema (BS) with a type declared abstract, then another 
> schema (DS) that declares a type based on that abstract type.
>
> I use XSD to generate code for each schema. I am using the 
> --generate-polymorphic flag.
>
> The code for BS is compiled into a library (BL).
> When the code for DS is compiled into an exe (E1) and statically 
> linked to BL there is no problem.
> But when the code for DS is compiled into a library (DL) and then 
> another exe is created (E2) which links to both BL and DL with GCC, 
> again, there is no problem but with MSVC the exe (E2) fails to parse 
> valid xml files.
>
> I have been stepping through the XSD source code and generated code 
> while running E2 and have discovered the following:
> - An exception of type no_type_info<> is thrown. It simple contains 
> the message:
>    no type information available for type ''
> - This exception is thrown from type_factory_map<>::find_type() as 
> that function tries to find the factory for the derived type in DS.
> - It appears that XSD generated code generates a static variable of 
> type ::xsd::cxx::tree::type_factory_initializer<>  for declared types 
> whose constructor has the job of inserting a factory for that type 
> into type_factory_map::type_map_. So factories for all types should be 
> installed even before main is called.
> - It seems that with MSVC the constructor of this static variable is 
> never called when that var is declared in an object file build into a 
> lib (but it is called when built into an exe).
>
> The result of this is that E2 fails to parse a valid xml file even 
> though E1 does.
>
> Has anyone seen this problem before? Is it really conceivable that 
> static variables aren't getting initialised in a lib? Is there a 
> better work-around than just doing away with the libs and building all 
> the code straight into the final exe?
>
> Thanks,
>
> CYA
>
>




More information about the xsd-users mailing list