Connecting Tech Pros Worldwide Forums | Help | Site Map

How to avoid compiler/linker optimization

Roland Zerek
Guest
 
Posts: n/a
#1: Nov 16 '05
Hi,

Is there any way to avoid the compiler/linker to optimize one (and only
this) class member? Below is the code:

<code>
class MyFactory
{
/* . . . */
public static void Register( MyPrototype Proto ) { /* . . . */ }
public static MyPrototype Create( string Name ) { /* . . . */ }
}

class MyPrototype{
/* . . . */
public abstract MyPrototype Clone();
}

class MyClass : MyPrototype
{
/* . . . */
protected static MyClass sm_Instance;
protected MyClass() { MyFactory.Register( this ); }
public MyPrototype Clone() { return (MyPrototype)this; }
}
</code>

Classical Prototype project template. At least in C++. However the compiler
says that the MyClass.sm_Instance member is unused and does not take into
further consideratins. But this member has to guarranty that the prototype
registers itself in the prototype manager (factory). How to solve this
problem?

TIA

--
Roland




Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: How to avoid compiler/linker optimization


Roland Zerek <nobody@nowhere.com> wrote:[color=blue]
> Is there any way to avoid the compiler/linker to optimize one (and only
> this) class member? Below is the code:
>
> <code>
> class MyFactory
> {
> /* . . . */
> public static void Register( MyPrototype Proto ) { /* . . . */ }
> public static MyPrototype Create( string Name ) { /* . . . */ }
> }
>
> class MyPrototype{
> /* . . . */
> public abstract MyPrototype Clone();
> }
>
> class MyClass : MyPrototype
> {
> /* . . . */
> protected static MyClass sm_Instance;
> protected MyClass() { MyFactory.Register( this ); }
> public MyPrototype Clone() { return (MyPrototype)this; }
> }
> </code>
>
> Classical Prototype project template. At least in C++. However the compiler
> says that the MyClass.sm_Instance member is unused and does not take into
> further consideratins. But this member has to guarranty that the prototype
> registers itself in the prototype manager (factory). How to solve this
> problem?[/color]

How does MyClass.sm_Instance guarantee that the prototype registers
itself? I don't see how it achieves that at all.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Roland Zerek
Guest
 
Posts: n/a
#3: Nov 16 '05

re: How to avoid compiler/linker optimization


> > [...] Classical Prototype project template. At least in C++. However the
compiler[color=blue][color=green]
> > says that the MyClass.sm_Instance member is unused and does not take[/color][/color]
into[color=blue][color=green]
> > further consideratins. But this member has to guarranty that the[/color][/color]
prototype[color=blue][color=green]
> > registers itself in the prototype manager (factory). How to solve this
> > problem?[/color][/color]
[color=blue]
> How does MyClass.sm_Instance guarantee that the prototype registers
> itself? I don't see how it achieves that at all.[/color]

OK - the idea is that the static member is initialized at the beginning. So
if the static member is being initialized its constructor is being invoked
and the constructor registers itself in the manager for later use...

However, I am advanced C++ user and rather novice C# so I assumed that the
static member is being initialized at the very beginning of an application.
But it seems it does not. Instead of it it is being initialized at the first
refferrencing try. In fact my idea was not to explicitly do it.

But I still would like to have code that initializes itself at the
beginning. Do any C# solutions exist?

--
Roland


Roland Zerek
Guest
 
Posts: n/a
#4: Nov 16 '05

re: How to avoid compiler/linker optimization


An error occurred in my code
[color=blue][color=green]
> > class MyClass : MyPrototype
> > {
> > /* . . . */[/color][/color]
[color=blue][color=green]
> > protected static MyClass sm_Instance;[/color][/color]

The line above should present as follow
protected static MyClass sm_Instance = new MyClass();
[color=blue][color=green]
> > protected MyClass() { MyFactory.Register( this ); }
> > public MyPrototype Clone() { return (MyPrototype)this; }
> > }[/color][/color]

--
Roland


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 16 '05

re: How to avoid compiler/linker optimization


Roland Zerek <nobody@nowhere.com> wrote:[color=blue][color=green]
> > How does MyClass.sm_Instance guarantee that the prototype registers
> > itself? I don't see how it achieves that at all.[/color]
>
> OK - the idea is that the static member is initialized at the beginning. So
> if the static member is being initialized its constructor is being invoked
> and the constructor registers itself in the manager for later use...
>
> However, I am advanced C++ user and rather novice C# so I assumed that the
> static member is being initialized at the very beginning of an application.
> But it seems it does not. Instead of it it is being initialized at the first
> refferrencing try. In fact my idea was not to explicitly do it.[/color]

Type initializers are executed either "some time before any static
field is first referenced" or "at the first access to any static member
or constructor" depending on the beforefieldinit flag.

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information.
[color=blue]
> But I still would like to have code that initializes itself at the
> beginning. Do any C# solutions exist?[/color]

Well, you could write some code to examine all the types in all the
loaded assemblies, looking for (say) a specific attribute which you
define to mean "call my type initializer immediately". You could then
hook into AppDomain.AssemblyLoad to apply that procedure to any freshly
loaded assembly too.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread