1

現在のシステム時刻を取得するためにブースト ライブラリを使用していますが、コードは機能しますが、プログラムの後に Visualt Studio 2010 が終了します。存在しないポインターを解放しようとすると、デバッガーが中断します。これはboostのネイティブコードが原因であることはわかっています。 コードのboost部分をコメントしてもエラーは発生しないためです。

これまで、MSDN で説明されているように #pragma を使用してみましたが、成功しませんでした。(GetSystemTime 関数を使用して時間を取得しようとしましたが、boost のようなマイクロ秒の詳細を取得できません。)

マイコード

#pragma managed(push, off)
void GetSystemDateTime(SDateTime& stimeblock);
#pragma managed(pop)

int main()
{

c++/cli code

SDateTime stimestruct[1];
//call to the function having the boost code..
GetSystemDateTime(stimestruct[0]);

}

関数定義

 #pragma managed(push, off)
  void GetSystemDateTime(SDateTime& timeblock)
    {

   // SYSTEMTIME time;
   // GetSystemTime(&time);
   // WORD millis = (time.wSecond * 1000) + time.wMilliseconds;

    boost::posix_time::ptime now  = boost::posix_time::microsec_clock::local_time();
    std::tm pt_tm = to_tm(now);
    std::cout <<  now << std::endl;
    //std::cout <<  time.wYear<< time.wMonth<<time.wDay //<<time.wHour<<time.wMinute<<time.wSecond<<time.wMilliseconds << std::endl;
    std::string timestring =  to_iso_string(now);
    std::string sYear  = timestring.substr (0,4);
    std::string sMonth = timestring.substr (4,2);
    std::string sDay   = timestring.substr (6,2);
    std::string sHour  = timestring.substr (9,2);
    std::string sMinute = timestring.substr (11,2);
    std::string sSecond = timestring.substr (13,2);
    std::string sUSecond = timestring.substr (16);

    istringstream isYear(sYear);
    istringstream isMonth(sMonth);
    istringstream isDay(sDay);
    istringstream isHour(sHour);
    istringstream isMinute(sMinute);
    istringstream isSec(sSecond);
    istringstream isUSec(sUSecond);
    // use is like an input stream
    int iYear,iMonth,iDay,iHour,iMinute,iSecond,iUSecond;
    isYear >> iYear;
    isMonth >>iMonth;
    isDay >>iDay;
    isHour >>iHour;
    isMinute >>iMinute;
    isSec >>iSecond;
    isUSec >>iUSecond;


    timeblock.uiYear = iYear;
    timeblock.usiMonth = time.wMonth;
    timeblock.usiDay = time.wDay;
    timeblock.usiHour = time.wHour;
    timeblock.usiMinute = time.wMinute;
    timeblock.usiSec = time.wSecond;

    timeblock.udiUSec = time.wMilliseconds;

    // Display version information

    }
4

3 に答える 3

2

C++/CLI アセンブリのネイティブ コードで静的変数を使用すると、このエラーが発生することがわかりました。

私が見つけた唯一の回避策は、クラスまたはファイル スコープに移動するなどして、静的変数を削除することでした。

ただし、この静的変数がブースト コードにある場合、そうするのは簡単/不可能な場合があります。その場合、 を/clr使用せずにコンパイルされた別の C++ ファイルを作成し、そのファイルで boost 関数を使用して、それを C++/CLI アセンブリにリンクすることができます。

このエラーは、コンパイラが誤ったコードを生成したことが原因のようです。Microsoft にバグを報告しましたが、これは「修正されません」とクローズされましたが、コンパイラ チームは応答の中で他のいくつかの回避策を示しました。

于 2012-05-04T05:55:54.963 に答える
0

使ってみて

#pragma managed(push, off)
#pragma managed(pop)

#includeすべてのブースト ヘッダー ファイルの行の周り。

于 2012-05-04T04:40:05.077 に答える
0

私は数日間同じ問題を抱えています。

これは私が見つけた最良の回避策です。また、なぜこれが起こっているのかを説明します。

最後を見てください(7番と9番)

これが役立つことを願っていますhttp://www.codeproject.com/Articles/442784/Best-gotchas-of-Cplusplus-CLI

于 2013-03-17T22:30:00.713 に答える