2017-10-04 67 views
0

在我的應用程序中,我使用coroutine2來生成一些對象,我必須從流中解碼。這些對象是使用協程生成的。我的問題是,只要我到達流的末尾,理論上會拋出std :: ios_base :: failure,在特定條件下我的應用程序崩潰。boost,coroutine2(1.63.0):在32位窗口拋出異常崩潰visual studio

提供此功能的函數在C++中實現,作爲C函數導出並從C#調用。這一切都發生在Windows 10 x64的32位進程上。不幸的是,它只能在調試模式下從C#開始我的測試而沒有附加本地調試器時可靠地崩潰。只要我連接本地調試器,一切都按預期工作。

這裏是一個小的測試應用程序重現此問題:

Api.h

#pragma once 
extern "C" __declspec(dllexport) int __cdecl test(); 

Api.cpp

#include <iostream> 
#include <vector> 
#include <sstream> 
#include "Api.h" 

#define BOOST_COROUTINES2_SOURCE 
#include <boost/coroutine2/coroutine.hpp> 

int test() 
{ 
    using coro_t = boost::coroutines2::coroutine<bool>; 

    coro_t::pull_type source([](coro_t::push_type& yield) { 
     std::vector<char> buffer(200300, 0); 
     std::stringstream stream; 
     stream.write(buffer.data(), buffer.size()); 

     stream.exceptions(std::ios_base::eofbit | std::ios_base::badbit | std::ios_base::failbit); 

     try { 
      std::vector<char> dest(100100, 0); 
      while (stream.good() && !stream.eof()) { 
       stream.read(&dest[0], dest.size()); 
       std::cerr << "CORO: read: " << stream.gcount() << std::endl; 
      } 
     } 
     catch (const std::exception& ex) { 
      std::cerr << "CORO: caught ex: " << ex.what() << std::endl; 
     } 
     catch (...) { 
      std::cerr << "CORO: caught unknown exception." << std::endl; 
     } 
    }); 

    std::cout << "SUCCESS" << std::endl; 
    return 0; 
} 

C#:

using System; 
using System.Runtime.InteropServices; 
namespace CoroutinesTest 
{ 
    class Program 
    { 
     [DllImport("Api.dll", EntryPoint = "test", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] 
     internal static extern Int32 test(); 

     static void Main(string[] args) 
     { 
      test(); 
      Console.WriteLine("SUCCESS"); 
     } 
    } 
} 

一些細節:

  • 我們使用Visual Studio 2015 14並動態鏈接C++運行庫。
  • 測試庫靜態鏈接Boost 1.63.0。
  • 我們還嘗試通過直接從C++和python調用functionallity來重現此行爲。到目前爲止,這兩項測試都沒有成功。
  • 如果你用CTRL F5(意思是沒有.net調試器)啓動c#代碼,一切都會好起來的。只有當你用F5啓動它(意味着附加的.NET調試器)時,Visual Studio實例纔會崩潰。另外請確保不要啓用本地調試器!
  • 注意:如果我們不使用流中的例外,那麼所有接縫也都可以。不幸的是,解碼我的對象的代碼使用它們,因此我無法避免這種情況。

如果您對這裏可能出現的問題或解決方案有一些額外的提示,那將是驚人的。我不完全確定這是否是一個提升錯誤,也可能是c#調試器干擾boost-context。

在此先感謝!最好的問候,邁克爾

回答

-1

這只是一個猜測,但在你的協同程序中,我認爲你應該將一個布爾值推送到你的接收器(在代碼中命名爲yield)並且代碼沒有這樣做。

+0

這只是爲了簡化重現問題的示例。 這個問題發生之前,我們甚至得到這麼多。協程創建後,嘗試執行一些流操作,並在它甚至到達那個之前拋出異常。 – MichaelE1000

相關問題