I have a vb.NET application which sometimes craches and gives the error:
R6025 pure virtual function call
this is a run-time error in Visual C++
This is followed by:
LoaderLock was detected
Message: Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
And thats weird... cause I don't have any C++-code in this project. (unless NI implements in C++)
Does anybody know what could be the problem?
Peculiar. Just about every application has C++ code running somewhere because the CLR and Windows itself are written (at least portions of it) in C/C++. The LoaderLock issue is caused by someone attempting to use managed code in DllMain. When a DLL loads the OS takes a local temporarily to ensure the DLL is loaded thread-safe. However the CLR also needs to load some DLLs. Therefore if you are in the middle of loading your DLL and the CLR is then loaded it'll deadlock. This whole issue has been well documented. In fact in 2.0 I believe this issue has been resolved or at least worked around completely such that you don't have to worry about it.
Nevertheless if your application loads any DLLs then you need to ensure that each DLL does not attempt to load managed code in DllMain. Note that many ActiveX controls are written in C++ so instantiating an ActiveX control may be enough to cause the problem. The nice thing about this error though is that it should be pretty easy to track down the DLL causing problems if you are running through the debugger or if you run the application under Depends (with profiling) or another tool that logs DLL loading. The problem DLL will probably be the last DLL loaded. Of course if you are getting an actual exception then the exception should also tell you what is going on.
As for the pure virtual function error it is normally caused by attempting to use a method of a class in a DLL that is older than the public interface you wrote your code to. It theoretically can not happen if all the code is compiled because the compiler will catch such an error but with DLLs it is a little different. For example assume you create an interface called IMyInterface with a method called Foo and store it in the DLL MyInterface.dll. Now assume that you create a new DLL called v1.dll that contains a class that implements IMyInterface. Later on you extend IMyInterface with another method Bar. If you fail to update the v1.dll then at runtime when you call IMyInterface.Bar on the class from v1.dll it is an error because the function does not exist. Yet the compiler can't help you out because when you originally compiled v1.dll the Bar method did not exist.
Hope this helps,
Michael Taylor - 5/17/06