473,224 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Initialization of static variables in a class library

When does CLR performs initialization of static variables in a class
library?

(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...

It seems that (1) holds for unmanaged C++ code, but not for managed
code. I have class library with both managed and unmanaged static
variables that are not referenced by any part of the program. All the
unmanaged variables are initialized when the library is loaded, but the
constructors for the managed variables are never called. This is
problem because these constructors must be called in order to
initialize the application properly. Is there a way to trigger the
initialization of static variables in a class library?

--
Jesper

Oct 21 '06 #1
5 6732
When does CLR performs initialization of static variables in a class
library?

(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...

It seems that (1) holds for unmanaged C++ code, but not for managed
code. I have class library with both managed and unmanaged static
variables that are not referenced by any part of the program.
...
It's compiler specific behaviour that yours unmanaged static variables are
initialized. Compiler can optimize out such unreferenced variables
completely.

Vladimir Nesterovsky
Oct 21 '06 #2


On Oct 21, 1:10 pm, "Vladimir Nesterovsky"
<vladi...@nesterovsky-bros.comwrote:
When does CLR performs initialization of static variables in a class
library?
(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...
It seems that (1) holds for unmanaged C++ code, but not for managed
code. I have class library with both managed and unmanaged static
variables that are not referenced by any part of the program.
...It's compiler specific behaviour that yours unmanaged static variables are
initialized. Compiler can optimize out such unreferenced variables
completely.
Maybe, but on all the platforms that I work with (Windows, Linux and
VxWork) static variables in C++ are initialized as soon as the library
is loaded. I know of many C++ frameworks, which rely on these
constructor calls in order to install plug-ins, components, test cases
etc. What I am looking for here is a concrete way to explicit enforce
initialization of managed static variables in CLR without referring to
them.

--
Jesper

Oct 22 '06 #3
On Oct 21, 2:29 am, "Jesper Schmidt" <schmi...@gmail.comwrote:
When does CLR performs initialization of static variables in a class
library?

(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...

It seems that (1) holds for unmanaged C++ code, but not for managed
code. I have class library with both managed and unmanaged static
variables that are not referenced by any part of the program. All the
unmanaged variables are initialized when the library is loaded, but the
constructors for the managed variables are never called. This is
problem because these constructors must be called in order to
initialize the application properly. Is there a way to trigger the
initialization of static variables in a class library?
I have done some further investigation into my problem with the missing
initialization of unreferenced static variables in a class library. Out
of desperation, I tried to add the source code directly to the project,
which generates the final executable, and then suddenly everything
worked including the new .NET functionality I have added. Thank you,
Microsoft, for making the excellent .NET framework available to us C++
programmers. C++/CLI is great! Below is the stack trace that initiated
the initialization of the managed static variables.

_initterm_m(void*** pfbegin = 0x004051A4, pfend = error: cannot obtain
value)
<CrtImplementationDetails>::LanguageSupport::Initi alizePerProcess()
<CrtImplementationDetails>::LanguageSupport::_Init ialize()
<CrtImplementationDetails>::LanguageSupport::Initi alize()
..cctor@@$$FYMXXZ()

Can anybody tell me why the code should work any differently when it is
loaded as a class library or even better does anybody know how I move
the code back into the class library again without loosing the
initialization of the static variables?

--
Jesper

Oct 22 '06 #4
Jesper Schmidt wrote:
On Oct 21, 2:29 am, "Jesper Schmidt" <schmi...@gmail.comwrote:
>When does CLR performs initialization of static variables in a class
library?

(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...

It seems that (1) holds for unmanaged C++ code, but not for managed
code. I have class library with both managed and unmanaged static
variables that are not referenced by any part of the program. All the
unmanaged variables are initialized when the library is loaded, but
the constructors for the managed variables are never called. This is
problem because these constructors must be called in order to
initialize the application properly. Is there a way to trigger the
initialization of static variables in a class library?

I have done some further investigation into my problem with the
missing initialization of unreferenced static variables in a class
library. Out of desperation, I tried to add the source code directly
to the project, which generates the final executable, and then
suddenly everything worked including the new .NET functionality I
have added. Thank you, Microsoft, for making the excellent .NET
framework available to us C++ programmers. C++/CLI is great! Below is
the stack trace that initiated the initialization of the managed
static variables.

_initterm_m(void*** pfbegin = 0x004051A4, pfend = error: cannot
obtain value)
<CrtImplementationDetails>::LanguageSupport::Initi alizePerProcess()
<CrtImplementationDetails>::LanguageSupport::_Init ialize()
<CrtImplementationDetails>::LanguageSupport::Initi alize()
.cctor@@$$FYMXXZ()

Can anybody tell me why the code should work any differently when it
is loaded as a class library or even better does anybody know how I
move the code back into the class library again without loosing the
initialization of the static variables?
First thing I'd check - was the definition of the static variables and the
code that initializes them even included in the DLL? More than likely, the
linker simply left it out, since it's not referenced. In general, you have
to force a reference to something to be guaranteed that it's included in the
linked image.

-cd
Oct 23 '06 #5
Carl Daniel [VC++ MVP] wrote:
Jesper Schmidt wrote:
On Oct 21, 2:29 am, "Jesper Schmidt" <schmi...@gmail.comwrote:
When does CLR performs initialization of static variables in a class
library?

(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...

It seems that (1) holds for unmanaged C++ code, but not for managed
code. I have class library with both managed and unmanaged static
variables that are not referenced by any part of the program. All the
unmanaged variables are initialized when the library is loaded, but
the constructors for the managed variables are never called. This is
problem because these constructors must be called in order to
initialize the application properly. Is there a way to trigger the
initialization of static variables in a class library?
I have done some further investigation into my problem with the
missing initialization of unreferenced static variables in a class
library. Out of desperation, I tried to add the source code directly
to the project, which generates the final executable, and then
suddenly everything worked including the new .NET functionality I
have added. Thank you, Microsoft, for making the excellent .NET
framework available to us C++ programmers. C++/CLI is great! Below is
the stack trace that initiated the initialization of the managed
static variables.

_initterm_m(void*** pfbegin = 0x004051A4, pfend = error: cannot
obtain value)
<CrtImplementationDetails>::LanguageSupport::Initi alizePerProcess()
<CrtImplementationDetails>::LanguageSupport::_Init ialize()
<CrtImplementationDetails>::LanguageSupport::Initi alize()
.cctor@@$$FYMXXZ()

Can anybody tell me why the code should work any differently when it
is loaded as a class library or even better does anybody know how I
move the code back into the class library again without loosing the
initialization of the static variables?

First thing I'd check - was the definition of the static variables and the
code that initializes them even included in the DLL? More than likely, the
linker simply left it out, since it's not referenced. In general, you have
to force a reference to something to be guaranteed that it's included in the
linked image.
I have thought of this, but I do not think that this is the problem.
First, I have tried to switch on the "Keep Unreferenced Data
(/OPT:NOREF)" option to the linker and it made no difference. Secondly,
I can see the unreferenced symbol '*halo_unit_test_28' in the IL
disassembly (see below).

***************************************

..field static assembly method void *()
'?A0xfabd948e.halo_unit_test_28$initializer$' at D_00024424

..field static assembly valuetype
halo.unit_test.'test_case<halo::serialization::dot net::stream_obuffer_test>'
'?A0xfabd948e.halo_unit_test_28' at D_000369BC

..method assembly static void
'?A0xfabd948e.??__E?A0xfabd948e@halo_unit_test_28@ @YMXXZ'() cil managed
{
.vtentry 30 : 1
// Code size 36 (0x24)
.maxstack 4
IL_0000: ldsflda valuetype
halo.unit_test.'test_case<halo::serialization::dot net::stream_obuffer_test>'
'?A0xfabd948e.halo_unit_test_28'
IL_0005: ldsflda valuetype
'<CppImplementationDetails>'.$ArrayType$$$BY0DB@$$ CBD
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)
'?A0xfabd948e.unnamed-global-2'
IL_000a: ldsflda valuetype
'<CppImplementationDetails>'.$ArrayType$$$BY0DN@$$ CBD
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)
'?A0xfabd948e.unnamed-global-1'
IL_000f: ldc.i4.s 28
IL_0011: call valuetype
halo.unit_test.'test_case<halo::serialization::dot net::stream_obuffer_test>'*
modopt([mscorlib]System.Runtime.CompilerServices.CallConvThiscall)
'halo.unit_test.test_case<halo::serialization::dot net::stream_obuffer_test>.{ctor}'(valuetype
halo.unit_test.'test_case<halo::serialization::dot net::stream_obuffer_test>'*
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)
modopt([mscorlib]System.Runtime.CompilerServices.IsConst),

int8
modopt([mscorlib]System.Runtime.CompilerServices.IsSignUnspecifiedB yte)
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)*,

int8
modopt([mscorlib]System.Runtime.CompilerServices.IsSignUnspecifiedB yte)
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)*,

int32)
IL_0016: pop
IL_0017: ldftn void
'?A0xfabd948e.??__F?A0xfabd948e@halo_unit_test_28@ @YMXXZ'()
IL_001d: call int32 _atexit_m(method void *())
IL_0022: pop
IL_0023: ret
} // end of global method
'?A0xfabd948e.??__E?A0xfabd948e@halo_unit_test_28@ @YMXXZ'

..method assembly static void
'?A0xfabd948e.??__F?A0xfabd948e@halo_unit_test_28@ @YMXXZ'() cil managed
{
.vtentry 29 : 1
// Code size 11 (0xb)
.maxstack 1
IL_0000: ldsflda valuetype
halo.unit_test.'test_case<halo::serialization::dot net::stream_obuffer_test>'
'?A0xfabd948e.halo_unit_test_28'
IL_0005: call void
modopt([mscorlib]System.Runtime.CompilerServices.CallConvThiscall)
'halo.unit_test.test_case<halo::serialization::dot net::stream_obuffer_test>.{dtor}'(valuetype
halo.unit_test.'test_case<halo::serialization::dot net::stream_obuffer_test>'*
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)
modopt([mscorlib]System.Runtime.CompilerServices.IsConst))
IL_000a: ret
} // end of global method
'?A0xfabd948e.??__F?A0xfabd948e@halo_unit_test_28@ @YMXXZ'

***************************************

To me it looks as if the compiler has emitted the code that does the
initialization, but for some reason this code is not called when the
class library is loaded. Do CLR defer the initialization until the
symbol is referenced? If so, how do I explicitly trigger this
initialization without referring to the symbol itself?

--
Jesper

Oct 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
1
by: Richard Hayden | last post by:
Hi, I'm trying to port my O/S kernel from C to C++. I am using g++ with the following command-line switches: -Wall -fno-builtin -fno-rtti -fno-exceptions -fno-enforce-eh-specs -nostartfiles...
5
by: Nils | last post by:
Hi. I ahve three classes A,B,C, where B and C are derived from A. A:>B,C. Furthermore each class has an static int variable with name id, static int id; Besides that the class A implements the...
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
4
by: mnowosad | last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time an executing code within an AppDomain references a class for the the first time since the AppDomain was created/loaded,...
9
by: Allen | last post by:
In a static library, there is a static variable definition. static CLogger::mapFile; In both EXE and DLL, I use the same code. CLogReader uses CLogger::mapFile to do some work. CLogReader...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.