boost in a mixed CLR assemby

I realize it is not officially supported, but has anyone had success using boost libraries in a mixed CLR assembly? I am using visual studio 2005. I have been trying to accomplish this in an applicatation that has a Windows Forms front end, whose enire back end is build of libraries written in standard C++, making extensive use of boost, especially smart pointers and threads. The specific problem I am having involved initializing static variables. For instance, I have a global variable: boost::mutex osmutex; The program compiles and links fine, but during static initialization, I receive an error indicating _CrtIsValidHeapPointer(pUserData) is invalid. Looking at the stack unwind, the calling code is the "dynamic initializer for 'osmutex'". My suspicion is that the boost library is linking to a different run time library than my mixed code, and that the pointer being rejected is happening as a result of it coming from a different heap. I would like try to check this hypothesis by building boost forcing it to link to the correct C++ runtime library used by mixed assemblies (msvcmrtd.dll), but I am not familar enough with the boost build system to do this.

Daniel J. Kelly wrote:
I realize it is not officially supported, but has anyone had success using boost libraries in a mixed CLR assembly? I am using visual studio 2005. I have been trying to accomplish this in an applicatation that has a Windows Forms front end, whose enire back end is build of libraries written in standard C++, making extensive use of boost, especially smart pointers and threads.
I've used Boost compiled to the CLR runtime with a C# frontend. There's an example doing that in the Boost.Sandbox (look under libs/math_functions).
The specific problem I am having involved initializing static variables. For instance, I have a global variable: boost::mutex osmutex; The program compiles and links fine, but during static initialization, I receive an error indicating _CrtIsValidHeapPointer(pUserData) is invalid. Looking at the stack unwind, the calling code is the "dynamic initializer for 'osmutex'". My suspicion is that the boost library is linking to a different run time library than my mixed code, and that the pointer being rejected is happening as a result of it coming from a different heap.
Could the mutex be being used before it's initialised? Remember that static variables are initialised in an unspecified order.
I would like try to check this hypothesis by building boost forcing it to link to the correct C++ runtime library used by mixed assemblies (msvcmrtd.dll), but I am not familar enough with the boost build system to do this.
How was the Boost thread lib built? If your code is using the CLR then you would need to build Boost.Threads using the CLR as well I suspect. The easiest way to do that is to add the thread lib sources to a dll CLR project in your IDE and then make sure that BOOST_THREAD_BUILD_DLL is defined in the project settings. HTH, John.

Thanks for the quick response. It seems your last suggestion holds the most promise. It appears that I do have a library mismatch problem. I removed all boost references in a test build, and ran into the same problem with the initialization of some other non POD, native types. Still don't have this resolved, but now it looks like I shouldn't continue this thread on this mailing list. I'll still take a look at the example you cited. Dan Kelly -----Original Message----- From: John Maddock [mailto:john@johnmaddock.co.uk] Sent: Saturday, March 31, 2007 12:33 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] boost in a mixed CLR assemby Daniel J. Kelly wrote:
I realize it is not officially supported, but has anyone had success using boost libraries in a mixed CLR assembly? I am using visual studio 2005. I have been trying to accomplish this in an applicatation that has a Windows Forms front end, whose enire back end is build of libraries written in standard C++, making extensive use of boost, especially smart pointers and threads.
I've used Boost compiled to the CLR runtime with a C# frontend. There's an example doing that in the Boost.Sandbox (look under libs/math_functions).
The specific problem I am having involved initializing static variables. For instance, I have a global variable: boost::mutex osmutex; The program compiles and links fine, but during static initialization, I receive an error indicating _CrtIsValidHeapPointer(pUserData) is invalid. Looking at the stack unwind, the calling code is the "dynamic initializer for 'osmutex'". My suspicion is that the boost library is linking to a different run time library than my mixed code, and that the pointer being rejected is happening as a result of it coming from a different heap.
Could the mutex be being used before it's initialised? Remember that static variables are initialised in an unspecified order.
I would like try to check this hypothesis by building boost forcing it to link to the correct C++ runtime library used by mixed assemblies (msvcmrtd.dll), but I am not familar enough with the boost build system to do this.
How was the Boost thread lib built? If your code is using the CLR then you would need to build Boost.Threads using the CLR as well I suspect. The easiest way to do that is to add the thread lib sources to a dll CLR project in your IDE and then make sure that BOOST_THREAD_BUILD_DLL is defined in the project settings. HTH, John.

Daniel J. Kelly wrote:
I realize it is not officially supported, but has anyone had success using boost libraries in a mixed CLR assembly? I am using visual studio 2005. I have been trying to accomplish this in an applicatation that has a Windows Forms front end, whose enire back end is build of libraries written in standard C++, making extensive use of boost, especially smart pointers and threads.
The specific problem I am having involved initializing static variables. For instance, I have a global variable: boost::mutex osmutex; The program compiles and links fine, but during static initialization, I receive an error indicating _CrtIsValidHeapPointer(pUserData) is invalid. Looking at the stack unwind, the calling code is the "dynamic initializer for 'osmutex'". My suspicion is that the boost library is linking to a different run time library than my mixed code, and that the pointer being rejected is happening as a result of it coming from a different heap.
I would like try to check this hypothesis by building boost forcing it to link to the correct C++ runtime library used by mixed assemblies (msvcmrtd.dll), but I am not familar enough with the boost build system to do this.
Linking to the static version of boost::threads causes problems if you compile with /clr. I believe this behavior was introduced with Visual Studio 2005 SP1. I reported the issue both here ( search gmane archive ) and to Microsoft Connect. Neither party seemed too interested in addressing the issue. A workaround is to use the dynamic version of boost::threads ( #define BOOST_THREAD_USE_DLL ). Of course then you have to cart around another DLL but it does appear to fix the problem. John
participants (4)
-
Dan Kelly
-
Daniel J. Kelly
-
John Dunn
-
John Maddock