Static Variable Causing "System.BadImageFormatException"? WTF?  
Author Message
Darren M. Bork





PostPosted: Common Language Runtime, Static Variable Causing "System.BadImageFormatException"? WTF? Top

After a long wild goose chase (which I began in another thread after allot of frustration) the following is happening to me in a project. It appears to be better to be in this thread area...

This code does not work... in my project (it's simplified)

public class myclass
sub LoadRequiredData()
Static iLoadedHour as integer = -1
if not iLoadedHour = hour(now) then 'reload the user data hourly and on the first pass.
'code here to load the user data from the database...
iLoadedHour=hour(now) 'set last loaded hour to check against
end if
end sub
end class

This code does work

public class myclass
Private iLoadedHour as integer = -1
sub LoadRequiredData()
if not iLoadedHour = hour(now) then 'reload the user data hourly and on the first pass.
'code here to load the user data from the database...
iLoadedHour=hour(now) 'set last loaded hour to check against
end if
end sub
end class

Can anyone explain this to me
Again the error that is being thrown is "System.BadImageFormatException", which seams to have nothing to do with it unless the compiler is generating invalid code inside the EXE of the project which causes the error at run time.

Other things to note about my project and the class above...
-I an using other static variables in subroutines inside and outside the class (not very many though, <20, project wide)
-Commenting out parts of the code seam to have no impact until the "static" is removed, then it works normally with the original code.

I've done things like this many times and never had an issue with it. My god, it's so simple.




.NET Development19  
 
 
Darren M. Bork





PostPosted: Common Language Runtime, Static Variable Causing "System.BadImageFormatException"? WTF? Top

I just discovered this appears to have something to do with the static declaration AND the fact that the class is a "Partial" class, 2 files, which I used to denote a specific purpose for some code...

So the actual error now appears like the following...

Friend class myclass
sub StaticRuotineWithErrorOnCall() ' this routine raises the BadImageFormatException if called.
Static iLoadedHour as integer = -1
if not iLoadedHour = hour(now) then 'reload the user data hourly and on the first pass.
'code here to load the user data from the database...
iLoadedHour=hour(now) 'set last loaded hour to check against
end if
end sub
end class

Partial class myclass
sub StaticRoutineWithNoErrorOnCall()
Static iLoadedHour as integer = -1 'This routine works while the other routine doesn't
if not iLoadedHour = hour(now) then 'reload the user data hourly and on the first pass.
'code here to load the user data from the database...
iLoadedHour=hour(now) 'set last loaded hour to check against
end if
end sub
end class

additional notes...
-Moving the subroutine with the static declaration from the partial class to the main class file/definitiion fixes the problem for some reason.
-If an Event is in the main class file has a static declaration in it (my code uses a "system.timers.timer") the event never fires in compiled code, but doesn't complain.

This has got to be some kind of compiler issue.
Sort of like the compiler only properly initializes the static variables in the first partial class or something.



 
 
nobugz





PostPosted: Common Language Runtime, Static Variable Causing "System.BadImageFormatException"? WTF? Top

I think you found a bug in the VB.NET compiler. Boil it down to the smallest example that generates this exception with the current version of VS2005, then post it to the Product Feedback website. If you post it here too, we can check whether SP1 fixes it.


 
 
Harish_MS





PostPosted: Common Language Runtime, Static Variable Causing "System.BadImageFormatException"? WTF? Top

As you already discovered, this issue is specific to static locals in partial classes. This is a known issue. We have fixed this in the SP which is in Beta currently. So you could install the SP Beta and use it. Alternatively, workarounds for this are what you have already discovered i.e. place all the methods/properties with static locals in the main class or avoid using partial classes when their methods/properties contain static locals.

There is another workaround too i.e. to use private fields instead of static locals in partial classes. But note that this workaround does not provide the thread safety that the compiler generated code provides for the explicit initialization of static locals (i.e. for the initialization in this example: static x as Integer = 1). So you can use this workaround only if you make the initialization thread safe or if your application is not multi-threaded or you know for sure that your methods are not accessed from multiple threads.

In case you are interested in the details, the issue here is that the VB compiler is not emitting member fields generated for static locals in partial classes into the final assembly. More specifically, for a given class defined as multiple partial classes, the compiler is emitting these fields correctly only for the first partial class in which it sees static locals.

Thanks,

Harish


 
 
Harish_MS





PostPosted: Common Language Runtime, Static Variable Causing "System.BadImageFormatException"? WTF? Top

Note that the "place all the methods/properties with static locals in the main class " workaround that I mentioned before will not work in some cases because of the nature of the bug. So please ignore this workaround.

thanks,

Harish


 
 
Darren M. Bork





PostPosted: Common Language Runtime, Static Variable Causing "System.BadImageFormatException"? WTF? Top

Great. I'm not crazy! Good to know.