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

Boris Kolpackov boris at codesynthesis.com
Tue Oct 10 02:10:32 EDT 2006


Hi Martin,

Martin Nickolas <marty at tibra.com.au> writes:

> 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.

I was under the impression that you are building dynamic libraries
on Windows and was going to investigate this further. But now it is
clear that you are using static libraries and the behavior you've
described is exactly what happens. MS link editor will only link
in a translation unit from a static library if you are referencing
a symbol from that translation unit. Otherwise it is ignored. Note
that some link-editors on UNIX platforms may exhibit the same
behavior (I guess they've fixed it in GNU binutils).

I cannot see off the top of my head what can be done in the generated
code to resolve this automatically.


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20061010/b9b4e5c9/attachment.pgp


More information about the xsd-users mailing list