简体   繁体   中英

Why do I get a Debug Assertion Failed Error when trying to execute a WinForms app that uses boost?

I am working in Microsoft Visual Studio 2010 Express.

I have created a new Windows Forms Application and added one line of code (immediately after the #pragma once line in Form1.h ):

#include "boost/regex.hpp"

To get this project to compile I change /clr:pure to /clr . Also, the include and library path were set to my boost build.

The application compiles, but as soon as I run it I get a Debug Assertion Failed error.

The error occurs on File: dgbheap.c on line: 1516
at Expression: _CrtIsValidHeapPointer(pUserData)

Here is the full code for Form1.h generated by VS:

#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) {
             }
    };
}

I understand that an assertion failure error occurs when the assertion is false, but what is causing this? Why does this include fail when I put it in a Windows Forms application, but not in a Console application?

Thank you, William

You have a few options:

  1. Use .NET's built-in regex class rather than Boost.Regex since you're already using managed code
  2. Use Boost.Xpressive rather than Boost.Regex; it's header only, so you don't run into compiler/linker config issues as you do with linked libraries
  3. Non-header-only Boost libraries are linked statically by default with VC++; link to Boost.Regex dynamically rather than statically. This is described in the Boost.Regex docs thusly: " Note that if you want to dynamically link to the regex library when using the dynamic C++ runtime, define BOOST_REGEX_DYN_LINK when building your project. "
  4. If you want to continue linking to Boost.Regex statically, then rebuild Boost.Regex with /clr ; you're linking an app built with /clr to a static library built without it — no good. To do this, when invoking bjam, pass asynch-exceptions=on and cxxflags="/clr" as arguments

I'd need a full stack trace (including module as well as function name) to know for sure, but I think you've linked with a Boost DLL which has statically linked the CRT. That's no good, /clr requires using dynamic CRT (MSVCP100.DLL, IIRC). By statically linking the CRT, boost gets its own separate heap, then some object is deallocated to a heap different from whence it came triggering this assertion.

Boost's APIs are very object-heavy and full of inline functions, so I can't imagine why anyone would ever build a Boost DLL using the static CRT. But that's the usual explanation for this failure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM