Initializing statics in native library

Hello. We have a native C++ .lib that we have created that uses a const string in it. The string is initialized to "". We have successfully linked this native library to a managed Windows C++ Forms project and have code in the form to instantiate one of the objects defined in the native library. However, when the Forms application starts, it attempts to instantiate the const string in the native library and a _CrtIsValidHeapPointer assertion fails. Has anyone seen this behavior Admittedly, I am new to .NET and have been looking for a compiler switch that might address this issue. Does anyone have a suggestion how we might successfully use this native library without modifying it or recompiling it It may come down to us begin forced to recompile the native library as a managed one but we would prefer to avoid that if possible. Thank you!

-Matthew




Answer this question

Initializing statics in native library

  • Daniel Carey

    Hi Ben - Sorry for the delay getting back to you. We are using the standard entry point generated with a Windows forms project:

    [STAThreadAttribute]
    int main(array<System::String ^> ^args)

    I have searched high and low for information on known issues related to this problem, but since I was not aware that the entry point played a role, I have not focused on it at all. Do you have a suggestion where I might get more information Thanks!

    -Matthew



  • Luke D

    Actually, come to think of it, if this does fix your problem, you should file a bug here: http://lab.msdn.microsoft.com/productfeedback/.

    I'm not sure if we've gotten any customer reported issues (we give customer reported issues a very high priority) with this yet.



  • Laxmi Narsimha Rao ORUGANTI MSFT

    Well, maybe they are not so well known ;). If you check in the CRT init code though (vc\crt\src\crtexe.c etc) you'll notice that the managed entry point (int main(array<System::String ^> ^args) has to have different code in order to start up the CLR.

    I believe there are a few outstanding issues where things that should be initialized prior to user global initialization don't get initialized until just before the user main gets called (this means that global/static initializers don't have everything initialized before their execution). Switching out the entry point to one of the standard ones (int main(), int mait(int argc, char** argv) etc) will give you the old standard init code path and should alleviate such problems.

    -Ben



  • lagu26537

    What entry point are you using in your managed app There are several known issues with global initialization using int main(Array...) as opposed to one of the standard entries (int main(int argc, char** argv)).

    Thanks,
    Ben



  • Initializing statics in native library