我如何解决这个Visual Studio编译器BUG? [英] How can I workaround this Visual Studio compiler BUG?

查看:181
本文介绍了我如何解决这个Visual Studio编译器BUG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是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和downcast
Object * o = static_cast< Object *>(this);
Base * b = dynamic_cast< Base *>(o);
std :: cout<< this:<这种<< after cast:<< b;
//应该是相同的地址
if(this!= b)
std :: cout< 检查地址:NOK;
else
std :: cout<< 检查地址:OK;
}
};

struct派生:public Base
{
int i;
};

int main()
{
Derived d;
std :: cout<< end arrived:;
std :: stringstream * ss = new std :: stringstream;
delete ss;
std :: cout<< 好;
}

EDIT

我有一个想法...所以我想将每个std :: stream替换为一个包装器,例如。 std2 :: stream,其中我动态分配他们到一个智能ptr,我编译该包装实现没有/ vd2开关。我会在第二天尝试...



所以我想要这样的东西

  // compile without / vd2 
#include< sstream>
#include< iostream>
#include< boost / scoped_ptr.hpp>

命名空间std2
{
class stringstream
{
public:
stringstream()
{
m_stream。 reset(new std :: stringstream);
}
template< typename T>
std :: stringstream& operator<<(const T& param)
{
* m_stream<<帕拉姆
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;
}


解决方案

发布一个链接到这个讨论和适应版本的代码来说明问题。我最初的结论这似乎工作正常仍然站在,因为在Gtk没有崩溃对我来说,但问题是显然仍然存在,可以随时显示自己。



因为你的代码是一个简化的例子来重现这个问题,我不能给你的具体情况的解决方法,除了你可能不想做的事情(更改你的编译器,不使用dynamic_cast等等。 )



从我的理解,/ vd2和标准库的部分是不兼容的。你不能使这项工作按你想要的方式。



编辑:您编辑了您的问题,建议将sstreams封装到不同的翻译单元中无/ vd2并修改您的代码以使用该包装器。这将给你两个或更多翻译单元编译不同的标志影响二进制接口。你正试图通过使用实现定义和脆弱的方法来解决编译器错误。虽然它可能工作,我不会相信。


This is the "famous" /vd2 bug in visual studio, more information ex.: http://mcdougalljonathan.blogspot.com/2011/12/visual-c-2010-stdistringstream-crash-in.html or google for "visual studio vd2 gtkmm" keywords..

So I have to produce a release with a code that have a lot of these pattern below with VS2010. Looks like impossible, I have 10 days left. Any idea?

#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";
}

EDIT

I have an idea... So I want to replace every std::stream to a wrapper, for ex. std2::stream, where I dynamically allocate them to a smart ptr, and I compile that wrapper implementation without the /vd2 switch. I will try it on next day...

So I want something like this

// 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;
}

解决方案

I've updated my blog post with a link to this discussion and an adapted version of your code to illustrate the problem. My initial conclusion of "this seems to be working fine" still stands, as in Gtk hasn't crashed on me yet, but the problem is obviously still present and could manifest itself anytime.

Because your code is a simplified example to reproduce the issue, I cannot give a workaround for your specific case, except for things you probably won't want to do (change your compiler, don't use dynamic_cast, etc.)

From what I understand, /vd2 and parts of the standard library are incompatible. You won't be able to make this work the way you want. You'll need to ditch one or the other.

edit: You edited your question to propose wrapping the sstreams into a different translation unit compiled without /vd2 and modifying your code to use that wrapper. This would give you two or more translation units compiled with different flags that affect the binary interface. You are trying to work your way around compiler bugs by using implementation-defined and brittle methods. Although it might work, I wouldn't trust it.

这篇关于我如何解决这个Visual Studio编译器BUG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆