开发者

Boost.Thread throws bad_alloc exception in VS2010

Upon including <boost/thread.hpp> I get this exception:

First-chance exception at 0x7c812afb in CSF.exe: Microsoft C++ exception: 
boost::exception_detail::clone_impl<boost::exception_detail::bad_alloc_> at memory location 0x0012fc3c..
First-chance exception at 0x7c812afb in CSF.exe: Microsoft C++ excepti开发者_如何学编程on: [rethrow] at memory location 0x00000000..

I can't catch it, breaking at the memory location brings me to kernel32.dll and at this point I cannot say what's going on but it appears that the exception is thrown after the program ends and VS is capable of catching it.

The testcase:

#include <boost/thread.hpp>

int main()
{
   return 0;
}

Compiler command line:

/I"I:\SophisPal\boost-1_43_0-vc10-32\include\boost-1_43" /Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\Client.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue

Linker command line:

/OUT:"C:\Documents and Settings\user\my documents\visual studio 2010\Projects\CSF\Release\Client.exe" /INCREMENTAL:NO /NOLOGO "I:\SophisPal\boost-1_43_0-vc10-32\lib\libboost_system-vc100-mt-1_43.lib" "I:\SophisPal\boost-1_43_0-vc10-32\lib\libboost_date_time-vc100-mt-1_43.lib" "I:\SophisPal\boost-1_43_0-vc10-32\lib\libboost_regex-vc100-mt-1_43.lib" "I:\SophisPal\boost-1_43_0-vc10-32\lib\libboost_thread-vc100-mt-1_43.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /MANIFEST /ManifestFile:"Release\Client.exe.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Documents and Settings\user\my documents\visual studio 2010\Projects\CSF\Release\Client.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /PGD:"C:\Documents and Settings\user\my documents\visual studio 2010\Projects\CSF\Release\Client.pgd" /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE


This sounds like a mismatch between the Boost static libraries and the header files. How did you install boost? Did you run the bootstrap.bat on the machine to build it? Did you modify the boost header files after compilation? Do you have a library directory reference in the VS 2008 global settings to a directory containing a different version of the boost libraries?

Put another way, have you tried a clean install of boost with the libraries compiled with bootstrap.bat and have you deleted all other boost implementations on your machine? Until you've done that, you're working on a less-than-solid foundation.

If you still have trouble after that, have you tried going to Debug->Exceptions and turning on breaking on first-chance C++ and Win32 exceptions so that you can see the call stack in the debugger? If that doesn't work, have you tried using the Win32 API SetUnhandledExceptionFilter(), passing in a callback that uses the MiniDumpWriteDump() API of DBGHELP.DLL to write out a mini-dump (*.dmp) of the process to a file that you can then post-mortem debug using the VS 2010 debugger?


I ran into a similar issue with version 4.6.1 of g++ - boost version 1.47.0

The error is very vague, does not point to any particular section of code. After a lot of efforts it was traced to addition of pragma pack(1) directive, but not closed before declaring STL templates and or boost library data member templates.

The fix was to remove all pragma pack(x) and use GNU style packing "__attribute__((packed))" for the required structures

Boost should better in pointing error / warnings while linking with its libraries.


This was by design in boost 1.43 but has been since fixed. See this thread for the details.


I recommend trying to create a new, empty, Win32 Console Application (I usually check the "Empty Project" checkbox as well so as not to get precompiled headers and such for small projects).

Add a .cpp file to the project and give it your contents.

In the project properties, under C/C++ -> General, in the Additional Include Directories box, add the path to your top-level Boost folder (the one that contains the folder named boost that has all the header files). In my case, it's z:\dev\boost_1_43_0; based on your post, yours is probably I:\SophisPal\boost-1_43_0-vc10-32\include\boost-1_43.

Also in the project properties, under Linker -> General, in the Additional Library Directories box, add the path to the folder containing the compiled Boost libraries. On my PC, this is z:\dev\boost_1_43_0\lib; based on your post, yours is probably I:\SophisPal\boost-1_43_0-vc10-32\lib.

You don't need to explicitly tell the linker which libraries you want to link against; the Boost headers use one of the Visual C++ #pragma directives to tell the linker which libraries need to be used.

With just that, you should be able to build and run both Debug and Release configurations. I went through exactly these steps on and had no issues.

If that works, I'd start modifying the project, one option at a time, until it matches your current project's configuration. Eventually something will make it stop working, then you'll know what the problem is.

If that doesn't work, I'd look at downloading the Boost source and building it again.


Some thoughts:

How do you know that the crash is occuring after main()?

What happens if you run from a debugger and configure VS to catch every exception (not just unhandled)?

Can you post a stack trace of the crash?


You should use a memory profiler like VLD (Windows) or Valgrind (Linux) to check if it can point to the bad allocation, maybe you have a version of the Boost library that contains an incompatibility bug with your current compiler and you might have to apply a fix.


You either have a bad build or messed up project settings since you've tried creating a new basic project and simply including the header I'd suggest recompiling boost. You can download the latest version 1.45 from sourceforge which is linked from boost.org. Grab the latest bjam while you're there. You can run the following commands from your visual studio command prompt to build boost in 64bit and 32bit:

bjam --build-type=complete --toolset=msvc-10.0 --stagedir=lib64 address-model=64 stage > x64-buildboost.log
RD bin.v2 /S /Q
bjam --build-type=complete --toolset=msvc-10.0 --stagedir=lib32 stage > x86-buildboost.log
RD bin.v2 /S /Q

On a side note if the CSF reference refers to Intel's Communications Services Framework open source telephony project I worked on that before and upgrading boost with that project back in the day broke more things than it fixed ... fair warning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜