Adding #include <boost/thread/mutex.hpp> breaks my ActiveX control?
Asked Answered
M

2

7

Is there a known problem with the boost::mutex header when used inside an ActiveX control?
(Boost version v1.39)

If I create an MFC ActiveX Control project in Visual Studio 2008 called "DefaultOCXControl" then I can build it, the control registers itself as part of the build, and can be inserted into the ActiveX Test Container as you would expect. All good.

If I then just add this line:

#include <boost/thread/mutex.hpp>

at the top of my DefaultOCXControlCtrl.h file and rebuild: the registration step at the end of the build fails with:

Debug Assertion Failed!
Program: C:\Windows\system32\regsvr32.exe
File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dllinit.cpp
Line: 587

The offending assertion looks like this:

void AFXAPI AfxCoreInitModule()
{
    ASSERT(AfxGetModuleState() != AfxGetAppModuleState());
    ...

Attempting to insert the control into the ActiveX Test Container now results in the same message. If I remove the line and rebuild then everything is fine - so it definitely seems to be something in that header file that causes the issue.

Mandamus answered 18/3, 2011 at 17:51 Comment(0)
S
7

Have you tried linking boost::thread dynamically (define BOOST_THREAD_DYN_LINK)?

I had this kind of problems when using boost::thread with a mixed C++/.NET project and linking just boost::thread dynamically was the solution (here a possible explanation: http://article.gmane.org/gmane.comp.lib.boost.user/22617/match=clr)

Shalom answered 18/3, 2011 at 22:59 Comment(1)
Thanks. This was the solution in the end. Apparently there is a known problem with boost::thread and MFC as they both hang clean-up off functions of _pRawDllMain, discussed on the boost mail archive here: lists.boost.org/threads-devel/2009/06/0476.php I migrated our static library that was using boost::thread to a DLL instead and that sorted the issue.Mandamus
P
7

Since Boost 1.52 you could try adding this line to your code, especially if you prefer static linking:

#include <boost/thread/win32/mfc_thread_init.hpp>

Source: Boost Ticket 8550

Prognosticate answered 13/1, 2016 at 14:28 Comment(5)
Works like a charm with boost 1.55 and VS2013.Colitis
Where should I put this line?Leipzig
@Leipzig If I'm not mistaken it does not really matter, as long as it gets compiled into the binary that is experiencing the problem.Prognosticate
shoud I include it in dll project or the project which is using the dll? Currently I have a EasyHook dll project B which depends on MFC static library project A(Use MFC in a Shared DLL), I added this line in project B, but doesn't work. One more error occurs: LNK2005 _DllMain@12 already defined in MSVCRTD.lib(dll_dllmain_stub.obj)Leipzig
According to the linked Boost Ticket adding it to your DLL should suffice. If it doesn't then probably this solution does not work for you, sorry.Prognosticate

© 2022 - 2024 — McMap. All rights reserved.