0

我编写了一些使用各种 C++ 库的 C++ 类。我制作了一个 Windows 窗体项目,并将其设置为成功使用我的类。但是,我最近制作了另一个 C++ 类,现在我一直得到:

A first chance exception of type 'System.AccessViolationException' occurred in TEST_OCU.exe

这导致:

 An unhandled exception of type 'System.TypeInitializationException' occurred in Unknown Module.
 Additional information: The type initializer for '<Module>' threw an exception.

程序甚至还没有开始运行,新的、引起问题的 C++ 类甚至还没有构建。如果我注释掉这个new调用,并且只有一个指向这个新 C++ 类的指针,那么一切都编译得很好。但是,如果我在某个地方做类似的事情:

 if(new_class_ptr != NULL)
    new_class_ptr->SomeFunction()  //It doesn't matter what function this is

这将再次引发这些违规行为


一些事实:

  • 编译和链接很好,这似乎是一个运行时问题。
  • 我的解决方案使用非托管 C++ 库和类(我已经编写),以及一个托管 C++ 表单。
  • 到目前为止,我还没有遇到任何问题,我已经成功使用了一些 C++ 库一段时间。这是由我最近编写的一个新的 C++ 类引起的。
  • 导致这些违规的 C++ 类用于std::ifstream读取文件。如果我注释掉std::ifstream input_file(filename);我的表单项目成功运行。
  • 如果我在一个简单的 Win32 项目中使用 C++ 类,它可以在std::ifstream.
  • 我有一种强烈的感觉,它与这个问题有关

有人可以提供任何建议吗?谢谢


编辑:我提供了我的表单代码的某些部分。RTSPConnection 工作得很好,有问题的类是 RTPStream

public ref class Form1 : public System::Windows::Forms::Form
{
public:
  // ... Lots of generated code here ...

//Calls I've written
    private: static RTSPConnection * rtsp_connection_ = NULL; //This class works
    private: static RTPStream * rtp_connection_ = NULL; //This class does not
    bool streaming_;
    System::Threading::Thread^ streaming_thread_;

    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
        if(rtsp_connection_ == NULL)
        {
            rtsp_connection_ = new RTSPConnection("rtsp://192.168.40.131:8554/smpte");
            streaming_ = false;
        }

            //if(rtp_connection_ == NULL)
            //   rtp_connection_ = new RTPStream("test");

    }

    private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
        if(rtsp_connection_ != NULL)
            rtsp_connection_->StopStreaming();
    }

    private: System::Void button1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
        if(!streaming_)
        {
            //Start Streaming
            streaming_thread_ = gcnew Thread(gcnew ThreadStart(&Form1::WorkerThreadFunc));
            streaming_thread_->Start();

            this->button1->Text = L"Stop Streaming";
            streaming_ = true;
        }
        else
        {
            //Stop Streaming
            if(rtsp_connection_ != NULL)
            rtsp_connection_->StopStreaming();

            //THIS CALL RIGHT HERE WILL THROW AN ACCESS VIOLATION
            if(rtp_connection_ != NULL)
                rtp_connection_->StopStreaming();
            this->button1->Text = L"Start Streaming";
             streaming_ = false;
        }
    }
 };
4

1 回答 1

0

这两种说法似乎相互矛盾:

程序甚至还没有开始运行,新的、引起问题的 C++ 类甚至还没有构建。

如果我注释掉新调用,并且只有一个指向这个新 C++ 类的指针,那么一切都编译得很好。

问:你能把代码贴在你称之为“新”的地方吗?还是您在称呼“新”-也许您的意思是“如果我注释掉我的新课程”?

问:您能否在您的构造函数中设置一个断点,查看堆栈跟踪,看看是谁在调用它?什么时候

========== 附录 ==========

我强烈不同意这种说法:

这一切都取决于这一行:std::ifstream input_file(filename); 其中文件名是一个 std::string。

我强烈同意此声明:

如果您有相互依赖的静态类成员并且它们没有按照您期望的顺序初始化,那么您在 C# 中会遇到几乎相同的错误。在 C++ 中,如果你有一个静态单例和另一个引用它的静态成员

调用“ifstream”本身不是问题。相反,在程序初始化之前以某种方式调用调用 ifstream 的类问题所在。

问:你在这门课上叫“新”吗?如果有,在哪里。请剪切/粘贴该代码。

根据 MSDN,您应该能够设置“混合模式调试”。我有很多不同的 MSVS 副本 :) ...但 MSVS 2010/C++ 并不是其中之一。请查看此文档:

于 2012-07-05T17:49:37.733 回答