這是「著名的」/VD2在Visual Studio中的錯誤的詳細信息,例: http://mcdougalljonathan.blogspot.com/2011/12/visual-c-2010-stdistringstream-crash-in.html 或谷歌爲「Visual Studio的VD2 gtkmm的」關鍵詞..我該如何解決這個Visual Studio編譯器BUG?
所以我不得不產生一個代碼發行版VS2010下面有很多這樣的模式。看起來不可能,我還剩10天。任何想法?
#include <iostream>
#include <sstream>
struct Object
{
virtual ~Object() {}
};
struct Base: virtual public Object
{
Base() :Object()
{
// upcast and downcast
Object* o = static_cast<Object*>(this);
Base* b = dynamic_cast<Base*>(o);
std::cout << " this: " << this << " after cast: " << b;
// should be the same address
if (this != b)
std::cout << " check address: NOK";
else
std::cout << " check address: OK ";
}
};
struct Derived: public Base
{
int i;
};
int main()
{
Derived d;
std::cout << " end arrived: ";
std::stringstream* ss = new std::stringstream;
delete ss;
std::cout << "OK";
}
編輯
我有一個想法......所以,我想每一個的std ::流替換到包裝,爲前。 std2 :: stream,在那裏我動態地將它們分配給一個智能ptr,並且我編譯了不包含/ vd2開關的包裝器實現。我會嘗試在第二天...
所以我想是這樣的
// compile without /vd2
#include <sstream>
#include <iostream>
#include <boost/scoped_ptr.hpp>
namespace std2
{
class stringstream
{
public:
stringstream()
{
m_stream.reset(new std::stringstream);
}
template<typename T>
std::stringstream& operator<<(const T& param)
{
*m_stream << param;
return *m_stream;
}
std::string str() const
{ return m_stream->str(); }
private:
boost::scoped_ptr<std::stringstream> m_stream;
};
}
int main()
{
std2::stringstream stream;
stream << "DDDD" << std::endl;
std::cout << stream.str() << std::endl;
return 0;
}
也許我讀錯了,但似乎鏈接的文章說2010年可能不需要/ vd2標誌? – crashmstr
相信我我試過了,沒有它就不能使用虛擬繼承部分。但有了它,流析構函數失敗了。 –