How to avoid compiler/linker optimization

  • Thread starter Thread starter Roland Zerek
  • Start date Start date
R

Roland Zerek

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 Zerek said:
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?

How does MyClass.sm_Instance guarantee that the prototype registers
itself? I don't see how it achieves that at all.
 
[...] Classical Prototype project template. At least in C++. However the
compiler
How does MyClass.sm_Instance guarantee that the prototype registers
itself? I don't see how it achieves that at all.

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?
 
An error occurred in my code

The line above should present as follow
protected static MyClass sm_Instance = new MyClass();
 
Roland Zerek said:
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.

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.
But I still would like to have code that initializes itself at the
beginning. Do any C# solutions exist?

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