473,410 Members | 1,952 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,410 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 6751
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.