使用Visual Studio 2015(更新3)时,我在一个较大的项目中遇到了问题,我将其归结为下面的最小示例。
其中我有一个包含std :: string [3]的Test类。可能会发生这种情况,以至于Test的构造函数引发异常,我想在顶级(主)处处理该异常。但是,从构造函数中抛出时,xmemory中的断言失败-在我看来,当尝试展开并释放std :: string [3]时(请参阅下面的控制台输出和堆栈跟踪)。永远不会到达catch语句,在构建的发行版中,它只是崩溃。
当我用单个std :: string替换std :: string [3]时,正确到达了catch语句。同样,在构造完成后从另一个从外部调用的成员函数引发异常时,如果使用std :: string [3],则会按预期达到catch块。
这是怎么回事? 任何帮助表示赞赏。
最小示例:
#include "stdafx.h"
#include <vcruntime_exception.h>
#include <string>
#include <iostream>
class Test {
public:
Test() {
// throw exception from constructor
throw std::exception("TestException");
}
~Test() {
std::cout << "Destruction" << std::endl;
}
//std::string mySingleString = "SingleTestString";
std::string myStrings[3] = { "String1", "TestString2", "AnotherTestString" };
};
int main()
{
try
{
Test* t = new Test();
}
catch (std::exception& e)
{
// this is never reached while std::string myStrings is not uncommented
std::cout << "Caught:" << e.what() << std::endl;
}
catch (...) {
std::cout << "Caught an alien!" << std::endl;
}
return 0;
}
控制台输出:
Ausnahme ausgelöst bei 0x773CDDC2 in ConsoleApplication1.exe: Microsoft C++-Ausnahme: std::exception bei Speicherort 0x0019FB8C.
"ConsoleApplication1.exe" (Win32): "C:\Windows\SysWOW64\kernel.appcore.dll" geladen. Symbole wurden geladen.
"ConsoleApplication1.exe" (Win32): "C:\Windows\SysWOW64\msvcrt.dll" geladen. Symbole wurden geladen.
"ConsoleApplication1.exe" (Win32): "C:\Windows\SysWOW64\rpcrt4.dll" geladen. Symbole wurden geladen.
"ConsoleApplication1.exe" (Win32): "C:\Windows\SysWOW64\sspicli.dll" geladen. Symbole wurden geladen.
"ConsoleApplication1.exe" (Win32): "C:\Windows\SysWOW64\cryptbase.dll" geladen. Symbole wurden geladen.
"ConsoleApplication1.exe" (Win32): "C:\Windows\SysWOW64\bcryptprimitives.dll" geladen. Symbole wurden geladen.
"ConsoleApplication1.exe" (Win32): "C:\Windows\SysWOW64\sechost.dll" geladen. Symbole wurden geladen.
Debug Assertion Failed!
Program: ...15\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe
File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0
Line: 100
Expression: "(_Ptr_user & (_BIG_ALLOCATION_ALIGNMENT - 1)) == 0" && 0
堆栈跟踪:
ConsoleApplication1.exe!std::_Deallocate(void * _Ptr, unsigned int _Count, unsigned int _Sz) Zeile 99 C++
ConsoleApplication1.exe!std::allocator<char>::deallocate(char * _Ptr, unsigned int _Count) Zeile 720 C++
ConsoleApplication1.exe!std::_Wrap_alloc<std::allocator<char> >::deallocate(char * _Ptr, unsigned int _Count) Zeile 988 C++
ConsoleApplication1.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Tidy(bool _Built, unsigned int _Newsize) Zeile 2260 C++
ConsoleApplication1.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() Zeile 1018 C++
ConsoleApplication1.exe!`eh vector destructor iterator'(void * ptr, unsigned int size, unsigned int count, void(*)(void *) destructor) C++
ConsoleApplication1.exe!_main() C++
ConsoleApplication1.exe!main() Zeile 27 C++
答案 0 :(得分:1)
在评论者的帮助下,这很可能是默认成员初始化程序和Visual Studio 2015的问题。
没有默认的初始化程序就可以使用。