这是Visual Studio中的“著名” / vd2错误,更多信息,例如:
http://mcdougalljonathan.blogspot.com/2011/12/visual-c-2010-stdistringstream-crash-in.html
或在Google中输入“Visual Studio vd2 gtkmm”关键字。
因此,我必须使用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::stream替换为包装器,例如。 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;
}
最佳答案
可以为您解决问题的随机想法:
我没有看到更多选择,只是更改代码以在构造函数/析构函数中不使用dynamic_cast,并完全删除/ vd2