.net 为什么在尝试执行使用boost的WinForms应用程序时出现调试断言失败错误?

.net 为什么在尝试执行使用boost的WinForms应用程序时出现调试断言失败错误?,.net,winforms,debugging,boost,c++-cli,.net,Winforms,Debugging,Boost,C++ Cli,我在Microsoft Visual Studio 2010 Express中工作 我创建了一个新的Windows窗体应用程序,并添加了一行代码(紧跟在Form1.h中的#pragma once行之后): 为了让这个项目编译,我将/clr:pure更改为/clr。另外,include和library路径被设置为我的boost构建 应用程序可以编译,但我一运行它,就会收到一个调试断言失败错误 在第1516行的文件dgbheap.c上发生错误 at表达式:\u CrtIsValidHeapPoint

我在Microsoft Visual Studio 2010 Express中工作

我创建了一个新的Windows窗体应用程序,并添加了一行代码(紧跟在Form1.h中的
#pragma once
行之后):

为了让这个项目编译,我将
/clr:pure
更改为
/clr
。另外,include和library路径被设置为我的boost构建

应用程序可以编译,但我一运行它,就会收到一个调试断言失败错误

在第1516行的文件dgbheap.c上发生错误
at表达式:
\u CrtIsValidHeapPointer(pUserData)

以下是VS生成的Form1.h的完整代码:

#pragma once

#include "boost/regex.hpp"

namespace plz {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->SuspendLayout();
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(292, 273);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             }
    };
}
#pragma一次
#包括“boost/regex.hpp”
名称空间plz{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
/// 
///表格1摘要
/// 
public ref类Form1:public System::Windows::Forms::Form
{
公众:
表格1(无效)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
/// 
///清理所有正在使用的资源。
/// 
~Form1()
{
if(组件)
{
删除组件;
}
}
私人:
/// 
///必需的设计器变量。
/// 
系统::组件模型::容器^组件;
#pragma区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
void初始化组件(void)
{
此->SuspendLayout();
// 
//表格1
// 
这->自动缩放尺寸=系统::图纸::尺寸(6,13);
这->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
这->ClientSize=System::Drawing::Size(292273);
此->名称=L“Form1”;
此->文本=L“Form1”;
this->Load+=gcnew System::EventHandler(this,&Form1::Form1\u Load);
此->恢复布局(错误);
}
#布拉格端区
private:System::Void Form1_Load(System::Object^sender,System::EventArgs^e){
}
};
}
我知道当断言为false时会发生断言失败错误,但这是什么原因造成的呢?为什么当我将它放在Windows窗体应用程序中,而不是控制台应用程序中时,会包含fail

谢谢,,
威廉你有几个选择:

  • 使用而不是Boost.Regex,因为您已经在使用托管代码
  • 使用而不是Boost.Regex;它只是标题,所以不会像处理链接库那样遇到编译器/链接器配置问题
  • 默认情况下,使用VC++静态链接非标题Boost库;动态链接到Boost.Regex,而不是静态链接到Boost.Regex。这在THESILY中描述:“注意,如果您想在使用动态C++运行时动态链接到正则表达式库,则在创建项目时定义<代码> BooStRyExxDynLink < /代码>”
  • 如果要继续静态链接到Boost.Regex,请使用
    /clr
    重新构建Boost.Regex;您正在将一个使用
    /clr
    构建的应用程序链接到一个不使用它构建的静态库-这不好。为此,在调用bjam时,将
    asynchexceptions=on
    cxflags=“/clr”
    作为参数传递

  • 我需要一个完整的堆栈跟踪(包括模块和函数名)才能确定,但我认为您已经链接了一个Boost DLL,它静态链接了CRT。这不好,
    /clr
    需要使用动态CRT(MSVCP100.DLL,IIRC)。通过静态链接CRT,boost获得了它自己的独立堆,然后某个对象被释放到一个不同的堆中,从而触发此断言


    Boost的API非常重对象,并且充满了内联函数,所以我无法想象为什么有人会使用静态CRT构建Boost DLL。但这是这种失败的通常解释。

    首先为什么要这样做?您已经在使用托管代码,为什么不使用BCL?我正在从控制台应用程序移植代码。如果有人阅读此内容并选择选项4,请签出:@William Whispell:Edited以反映
    asynch exceptions=on
    的需要。将对象与
    /clr
    和不与
    /clr
    混合是完全正确的,并且受支持,其余的建议都是合理的。@Ben Voigt:通常,不总是。尝试Boost。例如,不使用
    /clr
    构建的线程静态链接到使用
    /clr
    构建的应用程序。@ildjarn:始终支持它。毕竟,它是从.NET环境调用现有C++代码的推荐方法。任何失败都代表错误。你有一个简单的复制我可以投票连接?
    #pragma once
    
    #include "boost/regex.hpp"
    
    namespace plz {
    
        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;
    
        /// <summary>
        /// Summary for Form1
        /// </summary>
        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
            Form1(void)
            {
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            }
    
        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~Form1()
            {
                if (components)
                {
                    delete components;
                }
            }
    
        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            void InitializeComponent(void)
            {
                this->SuspendLayout();
                // 
                // Form1
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(292, 273);
                this->Name = L"Form1";
                this->Text = L"Form1";
                this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
                this->ResumeLayout(false);
    
            }
    #pragma endregion
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 }
        };
    }